From 85a901ce95011611c8b28076ee9fd2fb4e241d74 Mon Sep 17 00:00:00 2001 From: mrg Date: Fri, 12 May 2006 00:51:13 +0000 Subject: pull across from GCC3 tree: >date: 2004/11/30 01:51:13; author: jwise; state: Exp; lines: +21 -3 >As discussed on tech-toolchain@netbsd.org, make cpp refuse to attempt to >parse a #include'd file which does not pass S_ISREG() if the environment variable >CPP_RESTRICTED is set. > >This is primarily intended for use by programs such as calendar(1) which >use cpp to parse untrusted user files -- without this change (and the corresponding >change to calendar(1)), any user can cause a denial-of-service for the daily >calendar -a run by #include'ing a named pipe. > >Many thanks to christos@netbsd for his help in polishing this. --- gnu/dist/gcc4/libcpp/files.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'gnu/dist/gcc4/libcpp') diff --git a/gnu/dist/gcc4/libcpp/files.c b/gnu/dist/gcc4/libcpp/files.c index 31c38d0bb80..91091968404 100644 --- a/gnu/dist/gcc4/libcpp/files.c +++ b/gnu/dist/gcc4/libcpp/files.c @@ -28,6 +28,7 @@ Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "mkdeps.h" #include "hashtab.h" #include "md5.h" +#include "../gcc/defaults.h" #include /* Variable length record files on VMS will have a stat size that includes @@ -191,6 +192,9 @@ static bool check_file_against_entries (cpp_reader *, _cpp_file *, bool); terminal by mistake (this can't happen on sane systems, but paranoia is a virtue). + We do, however, use nonblocking mode if CPP_RESTRICTED, since any + file which can block will be tossed out after fstat(). + Use the three-argument form of open even though we aren't specifying O_CREAT, to defend against broken system headers. @@ -200,20 +204,29 @@ static bool check_file_against_entries (cpp_reader *, _cpp_file *, bool); static bool open_file (_cpp_file *file) { + const char *cpp_restricted; + + GET_ENVIRONMENT(cpp_restricted, "CPP_RESTRICTED"); + if (file->path[0] == '\0') { file->fd = 0; set_stdin_to_binary_mode (); } else - file->fd = open (file->path, O_RDONLY | O_NOCTTY | O_BINARY, 0666); + file->fd = open (file->path, O_RDONLY | O_NOCTTY | O_BINARY + | (cpp_restricted != NULL) ? O_NONBLOCK : 0, 0666); if (file->fd != -1) { if (fstat (file->fd, &file->st) == 0) { - if (!S_ISDIR (file->st.st_mode)) + if (cpp_restricted != NULL + ? S_ISREG (file->st.st_mode) : !S_ISDIR (file->st.st_mode)) { + if (cpp_restricted) + fcntl(file->fd, F_SETFL, + fcntl(file->fd, F_GETFL, 0) & ~O_NONBLOCK); file->err_no = 0; return true; } -- cgit