File: | src/lib/libc/gen/popen.c |
Warning: | line 79, column 16 Call to function 'vfork' is insecure as it can lead to denial of service situations in the parent process. Replace calls to vfork with calls to the safer 'posix_spawn' function |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* $OpenBSD: popen.c,v 1.22 2019/06/28 13:32:41 deraadt Exp $ */ |
2 | /* |
3 | * Copyright (c) 1988, 1993 |
4 | * The Regents of the University of California. All rights reserved. |
5 | * |
6 | * This code is derived from software written by Ken Arnold and |
7 | * published in UNIX Review, Vol. 6, No. 8. |
8 | * |
9 | * Redistribution and use in source and binary forms, with or without |
10 | * modification, are permitted provided that the following conditions |
11 | * are met: |
12 | * 1. Redistributions of source code must retain the above copyright |
13 | * notice, this list of conditions and the following disclaimer. |
14 | * 2. Redistributions in binary form must reproduce the above copyright |
15 | * notice, this list of conditions and the following disclaimer in the |
16 | * documentation and/or other materials provided with the distribution. |
17 | * 3. Neither the name of the University nor the names of its contributors |
18 | * may be used to endorse or promote products derived from this software |
19 | * without specific prior written permission. |
20 | * |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
31 | * SUCH DAMAGE. |
32 | */ |
33 | |
34 | #include <sys/wait.h> |
35 | |
36 | #include <signal.h> |
37 | #include <errno(*__errno()).h> |
38 | #include <fcntl.h> |
39 | #include <unistd.h> |
40 | #include <stdio.h> |
41 | #include <stdlib.h> |
42 | #include <string.h> |
43 | #include <paths.h> |
44 | #include <wchar.h> |
45 | #include "thread_private.h" |
46 | |
47 | static struct pid { |
48 | struct pid *next; |
49 | FILE *fp; |
50 | pid_t pid; |
51 | } *pidlist; |
52 | |
53 | static void *pidlist_lock = NULL((void *)0); |
54 | |
55 | FILE * |
56 | popen(const char *program, const char *type) |
57 | { |
58 | struct pid * volatile cur; |
59 | FILE *iop; |
60 | int pdes[2]; |
61 | int target; |
62 | pid_t pid; |
63 | |
64 | if ((*type != 'r' && *type != 'w') || |
65 | (type[1] != '\0' && (type[1] != 'e' || type[2] != '\0'))) { |
66 | errno(*__errno()) = EINVAL22; |
67 | return (NULL((void *)0)); |
68 | } |
69 | |
70 | if ((cur = malloc(sizeof(struct pid))) == NULL((void *)0)) |
71 | return (NULL((void *)0)); |
72 | |
73 | if (pipe2(pdes, O_CLOEXEC0x10000) == -1) { |
74 | free(cur); |
75 | return (NULL((void *)0)); |
76 | } |
77 | |
78 | _MUTEX_LOCK(&pidlist_lock)do { if (__isthreaded) _thread_cb.tc_mutex_lock(&pidlist_lock ); } while (0); |
79 | switch (pid = vfork()) { |
Call to function 'vfork' is insecure as it can lead to denial of service situations in the parent process. Replace calls to vfork with calls to the safer 'posix_spawn' function | |
80 | case -1: /* Error. */ |
81 | _MUTEX_UNLOCK(&pidlist_lock)do { if (__isthreaded) _thread_cb.tc_mutex_unlock(&pidlist_lock ); } while (0); |
82 | (void)close(pdes[0]); |
83 | (void)close(pdes[1]); |
84 | free(cur); |
85 | return (NULL((void *)0)); |
86 | /* NOTREACHED */ |
87 | case 0: /* Child. */ |
88 | { |
89 | struct pid *pcur; |
90 | |
91 | /* |
92 | * because vfork() instead of fork(), must leak FILE *, |
93 | * but luckily we are terminally headed for an execl() |
94 | */ |
95 | for (pcur = pidlist; pcur; pcur = pcur->next) |
96 | close(fileno(pcur->fp)(!__isthreaded ? ((pcur->fp)->_file) : (fileno)(pcur-> fp))); |
97 | |
98 | target = *type == 'r'; |
99 | if (pdes[target] != target) { |
100 | if (dup2(pdes[target], target) == -1) |
101 | _exit(127); |
102 | } else { |
103 | int flags = fcntl(pdes[target], F_GETFD1); |
104 | if (flags == -1 || ((flags & FD_CLOEXEC1) && |
105 | fcntl(pdes[target], F_SETFD2, flags & ~FD_CLOEXEC1) |
106 | == -1)) |
107 | _exit(127); |
108 | } |
109 | |
110 | execl(_PATH_BSHELL"/bin/sh", "sh", "-c", program, (char *)NULL((void *)0)); |
111 | _exit(127); |
112 | /* NOTREACHED */ |
113 | } |
114 | } |
115 | _MUTEX_UNLOCK(&pidlist_lock)do { if (__isthreaded) _thread_cb.tc_mutex_unlock(&pidlist_lock ); } while (0); |
116 | |
117 | /* Parent; assume fdopen can't fail. */ |
118 | target = *type == 'w'; |
119 | iop = fdopen(pdes[target], type); |
120 | fwide(iop, -1); |
121 | (void)close(pdes[!target]); |
122 | |
123 | /* Link into list of file descriptors. */ |
124 | cur->fp = iop; |
125 | cur->pid = pid; |
126 | _MUTEX_LOCK(&pidlist_lock)do { if (__isthreaded) _thread_cb.tc_mutex_lock(&pidlist_lock ); } while (0); |
127 | cur->next = pidlist; |
128 | pidlist = cur; |
129 | _MUTEX_UNLOCK(&pidlist_lock)do { if (__isthreaded) _thread_cb.tc_mutex_unlock(&pidlist_lock ); } while (0); |
130 | |
131 | /* now that it's in the list, clear FD_CLOEXEC if unwanted */ |
132 | if (type[1] != 'e') { |
133 | int flags = fcntl(pdes[target], F_GETFD1); |
134 | if (flags != -1) |
135 | fcntl(pdes[target], F_SETFD2, flags & ~FD_CLOEXEC1); |
136 | } |
137 | |
138 | return (iop); |
139 | } |
140 | DEF_WEAK(popen)__asm__(".weak " "popen" " ; " "popen" " = " "_libc_popen"); |
141 | |
142 | /* |
143 | * pclose -- |
144 | * Pclose returns -1 if stream is not associated with a `popened' command, |
145 | * if already `pclosed', or waitpid returns an error. |
146 | */ |
147 | int |
148 | pclose(FILE *iop) |
149 | { |
150 | struct pid *cur, *last; |
151 | int pstat; |
152 | pid_t pid; |
153 | |
154 | /* Find the appropriate file pointer. */ |
155 | _MUTEX_LOCK(&pidlist_lock)do { if (__isthreaded) _thread_cb.tc_mutex_lock(&pidlist_lock ); } while (0); |
156 | for (last = NULL((void *)0), cur = pidlist; cur; last = cur, cur = cur->next) |
157 | if (cur->fp == iop) |
158 | break; |
159 | |
160 | if (cur == NULL((void *)0)) { |
161 | _MUTEX_UNLOCK(&pidlist_lock)do { if (__isthreaded) _thread_cb.tc_mutex_unlock(&pidlist_lock ); } while (0); |
162 | return (-1); |
163 | } |
164 | |
165 | /* Remove the entry from the linked list. */ |
166 | if (last == NULL((void *)0)) |
167 | pidlist = cur->next; |
168 | else |
169 | last->next = cur->next; |
170 | _MUTEX_UNLOCK(&pidlist_lock)do { if (__isthreaded) _thread_cb.tc_mutex_unlock(&pidlist_lock ); } while (0); |
171 | |
172 | (void)fclose(iop); |
173 | |
174 | do { |
175 | pid = waitpid(cur->pid, &pstat, 0); |
176 | } while (pid == -1 && errno(*__errno()) == EINTR4); |
177 | |
178 | free(cur); |
179 | |
180 | return (pid == -1 ? -1 : pstat); |
181 | } |
182 | DEF_WEAK(pclose)__asm__(".weak " "pclose" " ; " "pclose" " = " "_libc_pclose" ); |