[hackers] [sbase] cp: Only call chmod with -p or -a || Michael Forney

From: <git_AT_suckless.org>
Date: Fri, 14 Jul 2017 07:51:01 +0200 (CEST)

commit 51e432cc44310c503650d83269cef81df875f580
Author: Michael Forney <mforney_AT_mforney.org>
AuthorDate: Mon Jul 3 14:58:50 2017 -0700
Commit: Anselm R Garbe <garbeam_AT_gmail.com>
CommitDate: Fri Jul 14 07:50:50 2017 +0200

    cp: Only call chmod with -p or -a
    
    Previously, when the destination file was created with fopen, we needed
    to use fchmod to set its permissions.
    
    Now that we pass in the mode to creat, we already get the desired
    behavior of creating the file with the same mode as the source file
    modified by the user's file creation mask.
    
    This fixes the issue where a directory or special file created with
    mkdir/mknod does not end up with the appropriate mode with -p or -a
    (since it may have been narrowed by the umask).
    
    This also allows us to clear the SUID and SGID bits from the mode if the
    chown fails, as specified by POSIX.

diff --git a/libutil/cp.c b/libutil/cp.c
index 208a073..b6f8b23 100644
--- a/libutil/cp.c
+++ b/libutil/cp.c
_AT_@ -139,9 +139,6 @@ cp(const char *s1, const char *s2, int depth)
                         return 0;
                 }
 
- /* preserve permissions by default */
- fchmod(f2, st.st_mode);
-
                 close(f1);
                 close(f2);
         }
_AT_@ -155,12 +152,16 @@ cp(const char *s1, const char *s2, int depth)
                         cp_status = 1;
                 }
 
- /* owner */
+ /* owner and mode */
                 if (!S_ISLNK(st.st_mode)) {
                         if (chown(s2, st.st_uid, st.st_gid) < 0) {
                                 weprintf("chown %s:", s2);
                                 cp_status = 1;
- return 0;
+ st.st_mode &= ~(S_ISUID | S_ISGID);
+ }
+ if (chmod(s2, st.st_mode) < 0) {
+ weprintf("chmod %s:", s2);
+ cp_status = 1;
                         }
                 } else {
                         if (lchown(s2, st.st_uid, st.st_gid) < 0) {
Received on Fri Jul 14 2017 - 07:51:01 CEST

This archive was generated by hypermail 2.3.0 : Fri Jul 14 2017 - 08:01:02 CEST