From owner-svn-src-all@freebsd.org Mon Jul 29 19:02:18 2019 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 4A4BBB2AA4; Mon, 29 Jul 2019 19:02:18 +0000 (UTC) (envelope-from markj@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 042DE8FA7B; Mon, 29 Jul 2019 19:02:17 +0000 (UTC) (envelope-from markj@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id BF5DC4F47; Mon, 29 Jul 2019 19:02:17 +0000 (UTC) (envelope-from markj@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id x6TJ2HGb041791; Mon, 29 Jul 2019 19:02:17 GMT (envelope-from markj@FreeBSD.org) Received: (from markj@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id x6TJ2GfG041787; Mon, 29 Jul 2019 19:02:16 GMT (envelope-from markj@FreeBSD.org) Message-Id: <201907291902.x6TJ2GfG041787@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: markj set sender to markj@FreeBSD.org using -f From: Mark Johnston Date: Mon, 29 Jul 2019 19:02:16 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r350420 - in head: include lib/libc/stdio X-SVN-Group: head X-SVN-Commit-Author: markj X-SVN-Commit-Paths: in head: include lib/libc/stdio X-SVN-Commit-Revision: 350420 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 042DE8FA7B X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org X-Spamd-Result: default: False [-2.97 / 15.00]; local_wl_from(0.00)[FreeBSD.org]; NEURAL_HAM_MEDIUM(-0.99)[-0.994,0]; NEURAL_HAM_SHORT(-0.97)[-0.974,0]; ASN(0.00)[asn:11403, ipnet:2610:1c1:1::/48, country:US]; NEURAL_HAM_LONG(-1.00)[-1.000,0] X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 29 Jul 2019 19:02:18 -0000 Author: markj Date: Mon Jul 29 19:02:16 2019 New Revision: 350420 URL: https://svnweb.freebsd.org/changeset/base/350420 Log: Add mkostempsat(3). This is a variant of mkostemps() which takes a directory descriptor and returns a descriptor for a tempfile relative to that directory. Unlike the other mktemp functions, mkostempsat() can be used in capability mode. Reviewed by: cem Discussed with: brooks Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D21031 Modified: head/include/stdlib.h head/lib/libc/stdio/Makefile.inc head/lib/libc/stdio/Symbol.map head/lib/libc/stdio/mktemp.3 head/lib/libc/stdio/mktemp.c Modified: head/include/stdlib.h ============================================================================== --- head/include/stdlib.h Mon Jul 29 18:02:48 2019 (r350419) +++ head/include/stdlib.h Mon Jul 29 19:02:16 2019 (r350420) @@ -297,6 +297,7 @@ int mergesort_b(void *, size_t, size_t, int (^)(const #endif int mkostemp(char *, int); int mkostemps(char *, int, int); +int mkostempsat(int, char *, int, int); void qsort_r(void *, size_t, size_t, void *, int (*)(void *, const void *, const void *)); int radixsort(const unsigned char **, int, const unsigned char *, Modified: head/lib/libc/stdio/Makefile.inc ============================================================================== --- head/lib/libc/stdio/Makefile.inc Mon Jul 29 18:02:48 2019 (r350419) +++ head/lib/libc/stdio/Makefile.inc Mon Jul 29 19:02:16 2019 (r350420) @@ -63,7 +63,7 @@ MLINKS+=getc.3 fgetc.3 getc.3 getc_unlocked.3 getc.3 g MLINKS+=getline.3 getdelim.3 MLINKS+=getwc.3 fgetwc.3 getwc.3 getwchar.3 MLINKS+=mktemp.3 mkdtemp.3 mktemp.3 mkstemp.3 mktemp.3 mkstemps.3 \ - mktemp.3 mkostemp.3 mktemp.3 mkostemps.3 + mktemp.3 mkostemp.3 mktemp.3 mkostemps.3 mktemp.3 mkostempsat.3 MLINKS+=open_memstream.3 open_wmemstream.3 MLINKS+=printf.3 asprintf.3 printf.3 dprintf.3 printf.3 fprintf.3 \ printf.3 snprintf.3 printf.3 sprintf.3 \ Modified: head/lib/libc/stdio/Symbol.map ============================================================================== --- head/lib/libc/stdio/Symbol.map Mon Jul 29 18:02:48 2019 (r350419) +++ head/lib/libc/stdio/Symbol.map Mon Jul 29 19:02:16 2019 (r350420) @@ -171,6 +171,10 @@ FBSD_1.5 { gets_s; }; +FBSD_1.6 { + mkostempsat; +}; + FBSDprivate_1.0 { _flockfile; _flockfile_debug_stub; Modified: head/lib/libc/stdio/mktemp.3 ============================================================================== --- head/lib/libc/stdio/mktemp.3 Mon Jul 29 18:02:48 2019 (r350419) +++ head/lib/libc/stdio/mktemp.3 Mon Jul 29 19:02:16 2019 (r350420) @@ -28,7 +28,7 @@ .\" @(#)mktemp.3 8.1 (Berkeley) 6/4/93 .\" $FreeBSD$ .\" -.Dd August 8, 2013 +.Dd July 29, 2019 .Dt MKTEMP 3 .Os .Sh NAME @@ -46,6 +46,8 @@ .Fn mkostemp "char *template" "int oflags" .Ft int .Fn mkostemps "char *template" "int suffixlen" "int oflags" +.Ft int +.Fn mkostempsat "int dfd" "char *template" "int suffixlen" "int oflags" .Ft char * .Fn mkdtemp "char *template" .In unistd.h @@ -126,6 +128,21 @@ function are told the length of the suffix string. .Pp The +.Fn mkostempsat +function acts the same as +.Fn mkostemps +but takes an additional directory descriptor as a parameter. +The temporary file is created relative to the corresponding +directory, or to the current working directory if the special +value +.Dv AT_FDCWD +is specified. +If the template path is an absolute path, the +.Fa dfd +parameter is ignored and the behavior is identical to +.Fn mkostemps . +.Pp +The .Fn mkdtemp function makes the same replacement to the template as in .Fn mktemp @@ -262,9 +279,10 @@ and is not specified by .St -p1003.1-2008 . The .Fn mkostemp , -.Fn mkstemps -and +.Fn mkstemps , .Fn mkostemps +and +.Fn mkostempsat functions do not conform to any standard. .Sh HISTORY A @@ -293,6 +311,10 @@ and .Fn mkostemps functions appeared in .Fx 10.0 . +The +.Fn mkostempsat +function appeared in +.Fx 13.0 . .Sh BUGS This family of functions produces filenames which can be guessed, though the risk is minimized when large numbers of @@ -308,10 +330,11 @@ and opening it for use (later in the user application) particularly dangerous from a security perspective. Whenever it is possible, -.Fn mkstemp -or +.Fn mkstemp , .Fn mkostemp -should be used instead, since it does not have the race condition. +or +.Fn mkostempsat +should be used instead, since they do not have the race condition. If .Fn mkstemp cannot be used, the filename created by Modified: head/lib/libc/stdio/mktemp.c ============================================================================== --- head/lib/libc/stdio/mktemp.c Mon Jul 29 18:02:48 2019 (r350419) +++ head/lib/libc/stdio/mktemp.c Mon Jul 29 19:02:16 2019 (r350420) @@ -49,17 +49,25 @@ __FBSDID("$FreeBSD$"); char *_mktemp(char *); -static int _gettemp(char *, int *, int, int, int); +static int _gettemp(int, char *, int *, int, int, int); static const unsigned char padchar[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; int +mkostempsat(int dfd, char *path, int slen, int oflags) +{ + int fd; + + return (_gettemp(dfd, path, &fd, 0, slen, oflags) ? fd : -1); +} + +int mkostemps(char *path, int slen, int oflags) { int fd; - return (_gettemp(path, &fd, 0, slen, oflags) ? fd : -1); + return (_gettemp(AT_FDCWD, path, &fd, 0, slen, oflags) ? fd : -1); } int @@ -67,7 +75,7 @@ mkstemps(char *path, int slen) { int fd; - return (_gettemp(path, &fd, 0, slen, 0) ? fd : -1); + return (_gettemp(AT_FDCWD, path, &fd, 0, slen, 0) ? fd : -1); } int @@ -75,7 +83,7 @@ mkostemp(char *path, int oflags) { int fd; - return (_gettemp(path, &fd, 0, 0, oflags) ? fd : -1); + return (_gettemp(AT_FDCWD, path, &fd, 0, 0, oflags) ? fd : -1); } int @@ -83,19 +91,19 @@ mkstemp(char *path) { int fd; - return (_gettemp(path, &fd, 0, 0, 0) ? fd : -1); + return (_gettemp(AT_FDCWD, path, &fd, 0, 0, 0) ? fd : -1); } char * mkdtemp(char *path) { - return (_gettemp(path, (int *)NULL, 1, 0, 0) ? path : (char *)NULL); + return (_gettemp(AT_FDCWD, path, (int *)NULL, 1, 0, 0) ? path : (char *)NULL); } char * _mktemp(char *path) { - return (_gettemp(path, (int *)NULL, 0, 0, 0) ? path : (char *)NULL); + return (_gettemp(AT_FDCWD, path, (int *)NULL, 0, 0, 0) ? path : (char *)NULL); } __warn_references(mktemp, @@ -108,7 +116,7 @@ mktemp(char *path) } static int -_gettemp(char *path, int *doopen, int domkdir, int slen, int oflags) +_gettemp(int dfd, char *path, int *doopen, int domkdir, int slen, int oflags) { char *start, *trv, *suffp, *carryp; char *pad; @@ -155,7 +163,7 @@ _gettemp(char *path, int *doopen, int domkdir, int sle for (; trv > path; --trv) { if (*trv == '/') { *trv = '\0'; - rval = stat(path, &sbuf); + rval = fstatat(dfd, path, &sbuf, 0); *trv = '/'; if (rval != 0) return (0); @@ -168,11 +176,11 @@ _gettemp(char *path, int *doopen, int domkdir, int sle } } + oflags |= O_CREAT | O_EXCL | O_RDWR; for (;;) { if (doopen) { - if ((*doopen = - _open(path, O_CREAT|O_EXCL|O_RDWR|oflags, 0600)) >= - 0) + *doopen = _openat(dfd, path, oflags, 0600); + if (*doopen >= 0) return (1); if (errno != EEXIST) return (0);