On Sun, Mar 29, 2015 at 09:50:48PM +0200, Alexander Bluhm wrote:

> Hi,
> 
> While doing make makesum in the ports framework, I realized that
> the distinfo file always gets permissions 0600.  It uses sort -o
> in a way that the output file will overwrite the input file.  In
> this case, sort creates a temp file with 0600 and replaces the
> output file at the end.
> 
> I would like semantics where the permissions are preserved.  So in
> case of overwriting a file, use the minimum of umask and original
> permissions.  While there, prevent overwriting files without write
> access.
> 
> comment/ok?

If you call umask(2), it's better to reset it to the old value after
you're done. 

But it's likely better to fchmod the file while we have it still open.

        -Otto

> 
> bluhm
> 
> Index: usr.bin/sort/sort.c
> ===================================================================
> RCS file: /data/mirror/openbsd/cvs/src/usr.bin/sort/sort.c,v
> retrieving revision 1.48
> diff -u -p -r1.48 sort.c
> --- usr.bin/sort/sort.c       20 Mar 2015 23:04:07 -0000      1.48
> +++ usr.bin/sort/sort.c       29 Mar 2015 18:16:19 -0000
> @@ -1194,6 +1194,17 @@ main(int argc, char *argv[])
>       }
>  
>       if (real_outfile) {
> +             struct stat sb;
> +             mode_t mask;
> +
> +             if (stat(real_outfile, &sb) < 0)
> +                     err(2, "%s", real_outfile);
> +             if (access(real_outfile, W_OK) < 0)
> +                     err(2, "write %s", real_outfile);
> +             mask = umask(0);
> +             umask(mask);
> +             chmod(outfile, sb.st_mode & ~mask & (S_IRWXU|S_IRWXG|S_IRWXO));
> +
>               unlink(real_outfile);
>               if (rename(outfile, real_outfile) < 0)
>                       err(2, "%s", real_outfile);

Reply via email to