Date: Wed, 25 Apr 2001 04:18:39 -0700 From: Kris Kennaway <kris@obsecurity.org> To: audit@FreeBSD.org Subject: pax mkstemp() fixes Message-ID: <20010425041839.C70021@xor.obsecurity.org>
next in thread | raw e-mail | index | archive | help
--6zdv2QT/q3FMhpsV Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable This is taken from OpenBSD. There are a lot of little style changes, typo fixes and gratuitous function renames which complicate the remaining diffs between the two versions; probably we should sync up with that, and then look at what's left (OpenBSD have maintained their pax(1) better than we have) Kris Index: extern.h =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: /mnt/ncvs/src/bin/pax/extern.h,v retrieving revision 1.8 diff -u -r1.8 extern.h --- extern.h 2001/04/17 07:46:37 1.8 +++ extern.h 2001/04/25 10:59:33 @@ -224,6 +224,9 @@ extern int docrc; extern char *dirptr; extern char *argv0; +extern char *tempfile; +extern char *tempbase; + int main __P((int, char **)); void sig_cleanup __P((int)); =20 Index: pax.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: /mnt/ncvs/src/bin/pax/pax.c,v retrieving revision 1.14 diff -u -r1.14 pax.c --- pax.c 2001/03/02 16:19:49 1.14 +++ pax.c 2001/04/25 11:05:47 @@ -55,6 +55,7 @@ #include <sys/resource.h> #include <errno.h> #include <locale.h> +#include <paths.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> @@ -96,6 +97,8 @@ int docrc; /* check/create file crc */ char *dirptr; /* destination dir in a copy */ char *argv0; /* root of argv[0] */ +char *tempfile; /* tempfile to use for mkstemp(3) */ +char *tempbase; /* basename of tempfile to use for mkstemp(3) */ sigset_t s_mask; /* signal mask for cleanup critical sect */ =20 /* @@ -228,7 +231,29 @@ char **argv; #endif { + char *tmpdir; + size_t tdlen; + (void) setlocale(LC_ALL, ""); + + /* + * Where should we put temporary files? + */ + if ((tmpdir =3D getenv("TMPDIR")) =3D=3D NULL || *tmpdir =3D=3D '\0') + tmpdir =3D _PATH_TMP; + tdlen =3D strlen(tmpdir); + while(tdlen > 0 && tmpdir[tdlen - 1] =3D=3D '/') + tdlen--; + tempfile =3D malloc(tdlen + 1 + sizeof(_TFILE_BASE)); + if (tempfile =3D=3D NULL) { + pax_warn(1, "Cannot allocate memory for temp file name."); + return(exit_val); + } + if (tdlen) + memcpy(tempfile, tmpdir, tdlen); + tempbase =3D tempfile + tdlen; + *tempbase++ =3D '/'; + /* * parse options, determine operational mode, general init */ Index: pax.h =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: /mnt/ncvs/src/bin/pax/pax.h,v retrieving revision 1.8 diff -u -r1.8 pax.h --- pax.h 1999/08/27 23:14:45 1.8 +++ pax.h 2001/04/25 11:00:53 @@ -237,3 +237,4 @@ #define HEX 16 #define OCT 8 #define _PAX_ 1 +#define _TFILE_BASE "paxXXXXXXXXXX" Index: tables.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: /mnt/ncvs/src/bin/pax/tables.c,v retrieving revision 1.13 diff -u -r1.13 tables.c --- tables.c 1999/08/27 23:14:46 1.13 +++ tables.c 2001/04/25 11:14:09 @@ -360,8 +360,6 @@ ftime_start() #endif { - char *pt; - if (ftab !=3D NULL) return(0); if ((ftab =3D (FTM **)calloc(F_TAB_SZ, sizeof(FTM *))) =3D=3D NULL) { @@ -373,16 +371,14 @@ * get random name and create temporary scratch file, unlink name * so it will get removed on exit */ - if ((pt =3D tempnam((char *)NULL, (char *)NULL)) =3D=3D NULL) - return(-1); - (void)unlink(pt); - - if ((ffd =3D open(pt, O_RDWR | O_CREAT, S_IRWXU)) < 0) { - sys_warn(1, errno, "Unable to open temporary file: %s", pt); + memcpy(tempbase, _TFILE_BASE, sizeof(_TFILE_BASE)); + if ((ffd =3D mkstemp(tempfile)) < 0) { + sys_warn(1, errno, "Unable to create temporary file: %s", + tempfile); return(-1); } + (void)unlink(tempfile); =20 - (void)unlink(pt); return(0); } =20 @@ -1210,22 +1206,19 @@ dir_start() #endif { - char *pt; - if (dirfd !=3D -1) return(0); - if ((pt =3D tempnam((char *)NULL, (char *)NULL)) =3D=3D NULL) - return(-1); =20 /* * unlink the file so it goes away at termination by itself */ - (void)unlink(pt); - if ((dirfd =3D open(pt, O_RDWR|O_CREAT, 0600)) >=3D 0) { - (void)unlink(pt); + memcpy(tempbase, _TFILE_BASE, sizeof(_TFILE_BASE)); + if ((dirfd =3D mkstemp(tempfile)) >=3D 0) { + (void)unlink(tempfile); return(0); } - pax_warn(1, "Unable to create temporary file for directory times: %s", pt= ); + pax_warn(1, "Unable to create temporary file for directory times: %s", + tempfile); return(-1); } =20 --6zdv2QT/q3FMhpsV Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.4 (FreeBSD) Comment: For info see http://www.gnupg.org iD8DBQE65rKOWry0BWjoQKURAvGKAJ96RgxVFaBqDJSzA2K5mg76wuGAzQCeIMlb Ll2iGldWIsIe/sKy9gH44qI= =DMF5 -----END PGP SIGNATURE----- --6zdv2QT/q3FMhpsV-- To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-audit" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20010425041839.C70021>