Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Thu, 8 Jun 2017 22:15:55 +0200
From: Szabolcs Nagy <nsz@...t70.net>
To: musl@...ts.openwall.com
Subject: Re: tmpfile patch

* Petr Skočík <pskocik@...il.com> [2017-06-08 20:01:53 +0200]:
> Hi,
> I made a patch to tmpfile() to make it signal-proof and use
> O_TMPFILE if it can.
> 
> Best Regards,
> Petr Skocik

> diff --git a/src/stdio/tmpfile.c b/src/stdio/tmpfile.c
> index 525090aa..34c3d487 100644
> --- a/src/stdio/tmpfile.c
> +++ b/src/stdio/tmpfile.c
> @@ -1,6 +1,7 @@
>  #include <stdio.h>
>  #include <fcntl.h>
>  #include "stdio_impl.h"
> +#include "pthread_impl.h"
>  
>  #define MAXTRIES 100
>  
> @@ -8,10 +9,23 @@ char *__randname(char *);
>  
>  FILE *tmpfile(void)
>  {
> +#ifdef O_TMPFILE

i don't think this ifdef helps,
kernel support for O_TMPFILE can
only be detected at runtime,
you will get ENOSYS on an old kernel.

> +	int fd;
> +	FILE *f = 0;
> +	fd = sys_open("/tmp", O_RDWR|O_TMPFILE);
> +	if (fd >= 0) {
> +		f = __fdopen(fd, "w+");
> +		if (!f) __syscall(SYS_close, fd);
> +	}
> +	return f;
> +#else
>  	char s[] = "/tmp/tmpfile_XXXXXX";
> +	FILE *f = 0;
>  	int fd;
> -	FILE *f;
>  	int try;
> +	sigset_t saved_mask;
> +
> +	pthread_sigmask(SIG_SETMASK, SIGALL_SET, &saved_mask);
>  	for (try=0; try<MAXTRIES; try++) {
>  		__randname(s+13);
>  		fd = sys_open(s, O_RDWR|O_CREAT|O_EXCL, 0600);
> @@ -21,12 +35,15 @@ FILE *tmpfile(void)
>  #else
>  			__syscall(SYS_unlinkat, AT_FDCWD, s, 0);
>  #endif
> +			pthread_sigmask(SIG_SETMASK, &saved_mask, 0);
>  			f = __fdopen(fd, "w+");
>  			if (!f) __syscall(SYS_close, fd);
>  			return f;
>  		}
>  	}
> +	pthread_sigmask(SIG_SETMASK, &saved_mask, 0);
>  	return 0;
> +#endif /*O_TMPFILE*/
>  }
>  
>  LFS64(tmpfile);

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.