| @@ -1,111 +1,111 @@ | | | @@ -1,111 +1,111 @@ |
1 | $NetBSD: patch-ae,v 1.5 2009/01/26 04:54:50 dholland Exp $ | | 1 | $NetBSD: patch-ae,v 1.6 2009/02/04 04:52:46 dholland Exp $ |
2 | | | 2 | |
3 | Fix insecure-temp-files. Replaces older nonworking patch with similar | | 3 | Fix insecure-temp-files. Replaces older nonworking patch with similar |
4 | intent. XXX inadequately tested, lack of time. | | 4 | intent. XXX inadequately tested, lack of time. |
5 | | | 5 | |
6 | Upstream: not sent upstream; inadequately tested, lack of time. | | 6 | Upstream: sent upstream by obache@: http://sourceforge.net/tracker/?func=detail&atid=406763&aid=2537314&group_id=32880 |
7 | | | 7 | |
8 | --- src/types.c.orig 2008-07-09 15:51:26.000000000 -0400 | | 8 | --- src/types.c.orig 2008-07-09 15:51:26.000000000 -0400 |
9 | +++ src/types.c 2009-01-25 23:36:47.000000000 -0500 | | 9 | +++ src/types.c 2009-01-25 23:36:47.000000000 -0500 |
10 | @@ -27,6 +27,7 @@ | | 10 | @@ -27,6 +27,7 @@ |
11 | #include <signal.h> | | 11 | #include <signal.h> |
12 | #include <sys/wait.h> | | 12 | #include <sys/wait.h> |
13 | #include <unistd.h> | | 13 | #include <unistd.h> |
14 | +#include <paths.h> | | 14 | +#include <paths.h> |
15 | | | 15 | |
16 | #include <stddef.h> | | 16 | #include <stddef.h> |
17 | | | 17 | |
18 | @@ -345,14 +346,18 @@ static FType * match_file(GList *list, D | | 18 | @@ -345,14 +346,18 @@ static FType * match_file(GList *list, D |
19 | void typ_identify_end(MainInfo *min, const gchar *path) | | 19 | void typ_identify_end(MainInfo *min, const gchar *path) |
20 | { | | 20 | { |
21 | const GList *here; | | 21 | const GList *here; |
22 | - gchar buf[MAXNAMLEN + 2], *temp_name; | | 22 | - gchar buf[MAXNAMLEN + 2], *temp_name; |
23 | + gchar buf[MAXNAMLEN + 2]; | | 23 | + gchar buf[MAXNAMLEN + 2]; |
24 | gint fd[2], len, status; | | 24 | gint fd[2], len, status; |
25 | pid_t file_pid; | | 25 | pid_t file_pid; |
26 | FType *type; | | 26 | FType *type; |
27 | + char tempnamebuf[64]; | | 27 | + char tempnamebuf[64]; |
28 | + int tempfd; | | 28 | + int tempfd; |
29 | | | 29 | |
30 | if(file_list == NULL) | | 30 | if(file_list == NULL) |
31 | return; | | 31 | return; |
32 | - if((temp_name = tmpnam(NULL)) == NULL) | | 32 | - if((temp_name = tmpnam(NULL)) == NULL) |
33 | + strcpy(tempnamebuf, _PATH_TMP "gentoo.XXXXXX"); | | 33 | + strcpy(tempnamebuf, _PATH_TMP "gentoo.XXXXXX"); |
34 | + tempfd = mkstemp(tempnamebuf); | | 34 | + tempfd = mkstemp(tempnamebuf); |
35 | + if (tempfd < 0) | | 35 | + if (tempfd < 0) |
36 | return; | | 36 | return; |
37 | if(pipe(fd) != 0) | | 37 | if(pipe(fd) != 0) |
38 | return; | | 38 | return; |
39 | @@ -360,20 +365,16 @@ void typ_identify_end(MainInfo *min, con | | 39 | @@ -360,20 +365,16 @@ void typ_identify_end(MainInfo *min, con |
40 | file_pid = fork(); | | 40 | file_pid = fork(); |
41 | if(file_pid == 0) | | 41 | if(file_pid == 0) |
42 | { | | 42 | { |
43 | - guint bits = 0U; | | 43 | - guint bits = 0U; |
44 | - | | 44 | - |
45 | - if(close(STDIN_FILENO) == 0) | | 45 | - if(close(STDIN_FILENO) == 0) |
46 | - { | | 46 | - { |
47 | - if(dup(fd[STDIN_FILENO]) == STDIN_FILENO) | | 47 | - if(dup(fd[STDIN_FILENO]) == STDIN_FILENO) |
48 | - bits |= (close(fd[STDIN_FILENO]) == 0); | | 48 | - bits |= (close(fd[STDIN_FILENO]) == 0); |
49 | - } | | 49 | - } |
50 | - if(close(STDOUT_FILENO) == 0) | | 50 | - if(close(STDOUT_FILENO) == 0) |
51 | - { | | 51 | - { |
52 | - if(open(temp_name, O_CREAT | O_RDWR, S_IRWXU) == STDOUT_FILENO) | | 52 | - if(open(temp_name, O_CREAT | O_RDWR, S_IRWXU) == STDOUT_FILENO) |
53 | - bits |= (close(fd[STDOUT_FILENO]) == 0) << 1; | | 53 | - bits |= (close(fd[STDOUT_FILENO]) == 0) << 1; |
54 | - } | | 54 | - } |
55 | - if(bits == 3U && chdir(path) == 0) | | 55 | - if(bits == 3U && chdir(path) == 0) |
56 | - execlp("file", "file", "-f", "-", NULL); | | 56 | - execlp("file", "file", "-f", "-", NULL); |
57 | + if (dup2(fd[0], STDIN_FILENO) < 0) | | 57 | + if (dup2(fd[0], STDIN_FILENO) < 0) |
58 | + _exit(EXIT_FAILURE); | | 58 | + _exit(EXIT_FAILURE); |
59 | + if (dup2(tempfd, STDOUT_FILENO) < 0) | | 59 | + if (dup2(tempfd, STDOUT_FILENO) < 0) |
60 | + _exit(EXIT_FAILURE); | | 60 | + _exit(EXIT_FAILURE); |
61 | + close(fd[0]); | | 61 | + close(fd[0]); |
62 | + close(fd[1]); | | 62 | + close(fd[1]); |
63 | + close(tempfd); | | 63 | + close(tempfd); |
64 | + if (chdir(path) < 0) | | 64 | + if (chdir(path) < 0) |
65 | + _exit(EXIT_FAILURE); | | 65 | + _exit(EXIT_FAILURE); |
66 | + execlp("file", "file", "-f", "-", NULL); | | 66 | + execlp("file", "file", "-f", "-", NULL); |
67 | _exit(EXIT_FAILURE); | | 67 | _exit(EXIT_FAILURE); |
68 | } | | 68 | } |
69 | else if(file_pid < 0) | | 69 | else if(file_pid < 0) |
70 | @@ -382,21 +381,21 @@ void typ_identify_end(MainInfo *min, con | | 70 | @@ -382,21 +381,21 @@ void typ_identify_end(MainInfo *min, con |
71 | return; | | 71 | return; |
72 | } | | 72 | } |
73 | /* We don't need the input part of the pipe, so close it. */ | | 73 | /* We don't need the input part of the pipe, so close it. */ |
74 | - close(fd[STDIN_FILENO]); | | 74 | - close(fd[STDIN_FILENO]); |
75 | + close(fd[0]); | | 75 | + close(fd[0]); |
76 | /* Now, we have file listening on pipe, so write all filenames to it. */ | | 76 | /* Now, we have file listening on pipe, so write all filenames to it. */ |
77 | for(here = file_list; here != NULL; here = g_list_next(here)) | | 77 | for(here = file_list; here != NULL; here = g_list_next(here)) |
78 | { | | 78 | { |
79 | len = g_snprintf(buf, sizeof buf, "%s\n", DP_SEL_NAME(here)); | | 79 | len = g_snprintf(buf, sizeof buf, "%s\n", DP_SEL_NAME(here)); |
80 | - write(fd[STDOUT_FILENO], buf, len); | | 80 | - write(fd[STDOUT_FILENO], buf, len); |
81 | + write(fd[1], buf, len); | | 81 | + write(fd[1], buf, len); |
82 | } | | 82 | } |
83 | - close(fd[STDOUT_FILENO]); | | 83 | - close(fd[STDOUT_FILENO]); |
84 | + close(fd[1]); | | 84 | + close(fd[1]); |
85 | waitpid(file_pid, &status, 0); | | 85 | waitpid(file_pid, &status, 0); |
86 | | | 86 | |
87 | if(WIFEXITED(status)) | | 87 | if(WIFEXITED(status)) |
88 | { | | 88 | { |
89 | FILE *in; | | 89 | FILE *in; |
90 | | | 90 | |
91 | - if((in = fopen(temp_name, "rt")) != NULL) | | 91 | - if((in = fopen(temp_name, "rt")) != NULL) |
92 | + if((in = fdopen(tempfd, "rt")) != NULL) | | 92 | + if((in = fdopen(tempfd, "rt")) != NULL) |
93 | { | | 93 | { |
94 | const gchar *fout; | | 94 | const gchar *fout; |
95 | gchar line[MAXNAMLEN + 256]; | | 95 | gchar line[MAXNAMLEN + 256]; |
96 | @@ -412,10 +411,14 @@ void typ_identify_end(MainInfo *min, con | | 96 | @@ -412,10 +411,14 @@ void typ_identify_end(MainInfo *min, con |
97 | } | | 97 | } |
98 | fclose(in); | | 98 | fclose(in); |
99 | } | | 99 | } |
100 | + else | | 100 | + else |
101 | + close(tempfd); | | 101 | + close(tempfd); |
102 | } | | 102 | } |
103 | + else | | 103 | + else |
104 | + close(tempfd); | | 104 | + close(tempfd); |
105 | g_list_free(file_list); | | 105 | g_list_free(file_list); |
106 | file_list = NULL; | | 106 | file_list = NULL; |
107 | - remove(temp_name); | | 107 | - remove(temp_name); |
108 | + remove(tempnamebuf); | | 108 | + remove(tempnamebuf); |
109 | } | | 109 | } |
110 | | | 110 | |
111 | /* ----------------------------------------------------------------------------------------- */ | | 111 | /* ----------------------------------------------------------------------------------------- */ |