Bug Summary

File:src/usr.bin/time/time.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

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.4 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name time.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/usr.bin/time/obj -resource-dir /usr/local/llvm16/lib/clang/16 -internal-isystem /usr/local/llvm16/lib/clang/16/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/usr.bin/time/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fcf-protection=branch -fno-jump-tables -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/scan/2024-01-11-140451-98009-1 -x c /usr/src/usr.bin/time/time.c
1/* $OpenBSD: time.c,v 1.26 2023/10/15 18:20:25 cheloha Exp $ */
2/* $NetBSD: time.c,v 1.7 1995/06/27 00:34:00 jtc Exp $ */
3
4/*
5 * Copyright (c) 1987, 1988, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <sys/time.h>
34#include <sys/resource.h>
35#include <sys/wait.h>
36#include <sys/sysctl.h>
37
38#include <err.h>
39#include <errno(*__errno()).h>
40#include <signal.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <unistd.h>
44
45__dead__attribute__((__noreturn__)) void usage(void);
46
47int
48main(int argc, char *argv[])
49{
50 struct rusage ru;
51 struct timespec before, after, during;
52 int ch, exitonsig, lflag, portableflag, status;
53 pid_t pid;
54
55 if (pledge("stdio proc exec", NULL((void *)0)) == -1)
56 err(1, "pledge");
57
58 exitonsig = lflag = portableflag = 0;
59
60 while ((ch = getopt(argc, argv, "lp")) != -1) {
61 switch(ch) {
62 case 'l':
63 lflag = 1;
64 break;
65 case 'p':
66 portableflag = 1;
67 break;
68 default:
69 usage();
70 }
71 }
72 argc -= optind;
73 argv += optind;
74
75 if (argc < 1)
76 usage();
77
78 clock_gettime(CLOCK_MONOTONIC3, &before);
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 warn("fork");
82 return 1;
83 case 0: /* child */
84 execvp(*argv, argv);
85 warn("%s", *argv);
86 _exit(errno(*__errno()) == ENOENT2 ? 127 : 126);
87 }
88
89 /* parent */
90 signal(SIGINT2, SIG_IGN(void (*)(int))1);
91 signal(SIGQUIT3, SIG_IGN(void (*)(int))1);
92 while (wait3(&status, 0, &ru) != pid)
93 continue;
94 clock_gettime(CLOCK_MONOTONIC3, &after);
95 if (WIFSIGNALED(status)(((status) & 0177) != 0177 && ((status) & 0177
) != 0)
)
96 exitonsig = WTERMSIG(status)(((status) & 0177));
97 if (!WIFEXITED(status)(((status) & 0177) == 0))
98 fprintf(stderr(&__sF[2]), "Command terminated abnormally.\n");
99 timespecsub(&after, &before, &during)do { (&during)->tv_sec = (&after)->tv_sec - (&
before)->tv_sec; (&during)->tv_nsec = (&after)->
tv_nsec - (&before)->tv_nsec; if ((&during)->tv_nsec
< 0) { (&during)->tv_sec--; (&during)->tv_nsec
+= 1000000000L; } } while (0)
;
100
101 if (portableflag) {
102 fprintf(stderr(&__sF[2]), "real %9lld.%02ld\n",
103 (long long)during.tv_sec, during.tv_nsec / 10000000);
104 fprintf(stderr(&__sF[2]), "user %9lld.%02ld\n",
105 (long long)ru.ru_utime.tv_sec, ru.ru_utime.tv_usec / 10000);
106 fprintf(stderr(&__sF[2]), "sys %9lld.%02ld\n",
107 (long long)ru.ru_stime.tv_sec, ru.ru_stime.tv_usec / 10000);
108 } else {
109 fprintf(stderr(&__sF[2]), "%9lld.%02ld real ",
110 (long long)during.tv_sec, during.tv_nsec / 10000000);
111 fprintf(stderr(&__sF[2]), "%9lld.%02ld user ",
112 (long long)ru.ru_utime.tv_sec, ru.ru_utime.tv_usec / 10000);
113 fprintf(stderr(&__sF[2]), "%9lld.%02ld sys\n",
114 (long long)ru.ru_stime.tv_sec, ru.ru_stime.tv_usec / 10000);
115 }
116
117 if (lflag) {
118 int hz;
119 long ticks;
120 int mib[2];
121 struct clockinfo clkinfo;
122 size_t size;
123
124 mib[0] = CTL_KERN1;
125 mib[1] = KERN_CLOCKRATE12;
126 size = sizeof(clkinfo);
127 if (sysctl(mib, 2, &clkinfo, &size, NULL((void *)0), 0) < 0)
128 err(1, "sysctl");
129
130 hz = clkinfo.hz;
131 ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
132 hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
133
134 fprintf(stderr(&__sF[2]), "%10ld %s\n",
135 ru.ru_maxrss, "maximum resident set size");
136 fprintf(stderr(&__sF[2]), "%10ld %s\n", ticks ? ru.ru_ixrss / ticks : 0,
137 "average shared memory size");
138 fprintf(stderr(&__sF[2]), "%10ld %s\n", ticks ? ru.ru_idrss / ticks : 0,
139 "average unshared data size");
140 fprintf(stderr(&__sF[2]), "%10ld %s\n", ticks ? ru.ru_isrss / ticks : 0,
141 "average unshared stack size");
142 fprintf(stderr(&__sF[2]), "%10ld %s\n",
143 ru.ru_minflt, "minor page faults");
144 fprintf(stderr(&__sF[2]), "%10ld %s\n",
145 ru.ru_majflt, "major page faults");
146 fprintf(stderr(&__sF[2]), "%10ld %s\n",
147 ru.ru_nswap, "swaps");
148 fprintf(stderr(&__sF[2]), "%10ld %s\n",
149 ru.ru_inblock, "block input operations");
150 fprintf(stderr(&__sF[2]), "%10ld %s\n",
151 ru.ru_oublock, "block output operations");
152 fprintf(stderr(&__sF[2]), "%10ld %s\n",
153 ru.ru_msgsnd, "messages sent");
154 fprintf(stderr(&__sF[2]), "%10ld %s\n",
155 ru.ru_msgrcv, "messages received");
156 fprintf(stderr(&__sF[2]), "%10ld %s\n",
157 ru.ru_nsignals, "signals received");
158 fprintf(stderr(&__sF[2]), "%10ld %s\n",
159 ru.ru_nvcsw, "voluntary context switches");
160 fprintf(stderr(&__sF[2]), "%10ld %s\n",
161 ru.ru_nivcsw, "involuntary context switches");
162 }
163
164 if (exitonsig) {
165 if (signal(exitonsig, SIG_DFL(void (*)(int))0) == SIG_ERR(void (*)(int))-1)
166 return 128 + exitonsig;
167 else
168 raise(exitonsig);
169 }
170 return WIFEXITED(status)(((status) & 0177) == 0) ? WEXITSTATUS(status)(int)(((unsigned)(status) >> 8) & 0xff) : EXIT_FAILURE1;
171}
172
173__dead__attribute__((__noreturn__)) void
174usage(void)
175{
176 fprintf(stderr(&__sF[2]), "usage: %s [-lp] utility [argument ...]\n",
177 getprogname());
178 exit(1);
179}