Bug Summary

File:src/usr.sbin/hostctl/hostctl.c
Warning:line 58, column 7
Although the value stored to 'sz' is used in the enclosing expression, the value is never actually read from 'sz'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name hostctl.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -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 -fno-rounding-math -mconstructor-aliases -munwind-tables -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.sbin/hostctl/obj -resource-dir /usr/local/lib/clang/13.0.0 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/usr.sbin/hostctl/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -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/vmm/scan-build/2022-01-12-194120-40624-1 -x c /usr/src/usr.sbin/hostctl/hostctl.c
1/* $OpenBSD: hostctl.c,v 1.5 2019/06/28 13:32:47 deraadt Exp $ */
2
3/*
4 * Copyright (c) 2016 Reyk Floeter <reyk@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <sys/ioctl.h>
20#include <sys/types.h>
21
22#include <dev/pv/pvvar.h>
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <unistd.h>
28#include <fcntl.h>
29#include <errno(*__errno()).h>
30#include <err.h>
31#include <vis.h>
32
33#define KVBUFSZ4096 4096 /* arbitrary value */
34
35char *path_pvbus = "/dev/pvbus0"; /* the first hv interface */
36int qflag = 0; /* quiet */
37int tflag = 0; /* show type */
38
39__dead__attribute__((__noreturn__)) void usage(void);
40int kvsetstr(char *, const char *, size_t);
41int kvsetfile(char *, const char *, size_t);
42
43__dead__attribute__((__noreturn__)) void
44usage(void)
45{
46 extern char *__progname;
47 fprintf(stderr(&__sF[2]), "usage: %s [-qt] [-f device] "
48 "[-i input] [-o output] key [value]\n", __progname);
49 exit(1);
50}
51
52int
53kvsetstr(char *dst, const char *src, size_t dstlen)
54{
55 size_t sz;
56
57 /* Sanitize the string before sending it to the kernel and host */
58 if ((sz = strnvis(dst, src, dstlen, VIS_SAFE0x20 | VIS_CSTYLE0x02)) >= dstlen)
Although the value stored to 'sz' is used in the enclosing expression, the value is never actually read from 'sz'
59 return (-1);
60
61 /* Remove trailing newline */
62 dst[strcspn(dst, "\n")] = '\0';
63
64 return (0);
65}
66
67int
68kvsetfile(char *dst, const char *input, size_t dstlen)
69{
70 char *buf = NULL((void *)0);
71 int ret = -1;
72 FILE *fp;
73
74 if (strcmp("-", input) == 0)
75 fp = stdin(&__sF[0]);
76 else if ((fp = fopen(input, "r")) == NULL((void *)0))
77 return (-1);
78
79 if ((buf = calloc(1, dstlen)) == NULL((void *)0))
80 goto done;
81 if (fread(buf, 1, dstlen - 1, fp) == 0)
82 goto done;
83 if (kvsetstr(dst, buf, dstlen) == -1)
84 goto done;
85
86 ret = 0;
87 done:
88 free(buf);
89 if (fp != stdin(&__sF[0]))
90 fclose(fp);
91 return (ret);
92}
93
94int
95main(int argc, char *argv[])
96{
97 const char *key, *value, *in = NULL((void *)0), *out = NULL((void *)0);
98 FILE *outfp = stdout(&__sF[1]);
99 int fd, ret;
100 struct pvbus_req pvr;
101 int ch;
102 unsigned long cmd = 0;
103 char *str;
104
105 while ((ch = getopt(argc, argv, "f:i:o:qt")) != -1) {
106 switch (ch) {
107 case 'f':
108 path_pvbus = optarg;
109 break;
110 case 'i':
111 in = optarg;
112 break;
113 case 'o':
114 out = optarg;
115 break;
116 case 'q':
117 qflag++;
118 break;
119 case 't':
120 tflag++;
121 break;
122 default:
123 usage();
124 }
125 }
126 argc -= optind;
127 argv += optind;
128
129 if ((fd = open(path_pvbus, O_RDONLY0x0000)) == -1)
130 err(1, "open: %s", path_pvbus);
131
132 if (out != NULL((void *)0)) {
133 if (strcmp("-", out) == 0)
134 outfp = stdout(&__sF[1]);
135 else if ((outfp = fopen(out, "w")) == NULL((void *)0))
136 err(1, "fopen: %s", out);
137 }
138
139 memset(&pvr, 0, sizeof(pvr));
140 pvr.pvr_keylen = pvr.pvr_valuelen = KVBUFSZ4096;
141 if ((pvr.pvr_key = calloc(1, pvr.pvr_keylen)) == NULL((void *)0) ||
142 (pvr.pvr_value = calloc(1, pvr.pvr_valuelen)) == NULL((void *)0))
143 err(1, "calloc");
144
145 if (tflag) {
146 if (ioctl(fd, PVBUSIOC_TYPE(((unsigned long)0x80000000|(unsigned long)0x40000000) | ((sizeof
(struct pvbus_req) & 0x1fff) << 16) | ((('V')) <<
8) | ((3)))
, &pvr, sizeof(pvr)) == -1)
147 err(1, "ioctl");
148
149 /* The returned type should be a simple single-line key */
150 if (stravis(&str, pvr.pvr_key,
151 VIS_WHITE(0x04 | 0x08 | 0x10) | VIS_DQ0x200 | VIS_CSTYLE0x02) == -1)
152 err(1, "stravis");
153 fprintf(outfp, "%s: %s\n", path_pvbus, str);
154 free(str);
155 goto done;
156 }
157
158 if (argc < 1)
159 usage();
160 key = argv[0];
161
162 if (kvsetstr(pvr.pvr_key, key, pvr.pvr_keylen) == -1)
163 errx(1, "key too long");
164
165 /* Validate command line options for reading or writing */
166 if (argc == 2 && in == NULL((void *)0)) {
167 cmd = PVBUSIOC_KVWRITE(((unsigned long)0x80000000|(unsigned long)0x40000000) | ((sizeof
(struct pvbus_req) & 0x1fff) << 16) | ((('V')) <<
8) | ((2)))
;
168 value = argv[1];
169 if (kvsetstr(pvr.pvr_value, value, pvr.pvr_valuelen) == -1)
170 errx(1, "value too long");
171 } else if (argc == 1 && in != NULL((void *)0)) {
172 cmd = PVBUSIOC_KVWRITE(((unsigned long)0x80000000|(unsigned long)0x40000000) | ((sizeof
(struct pvbus_req) & 0x1fff) << 16) | ((('V')) <<
8) | ((2)))
;
173 if (kvsetfile(pvr.pvr_value, in, pvr.pvr_valuelen) == -1)
174 errx(1, "input file");
175 } else if (argc == 1) {
176 cmd = cmd == 0 ? PVBUSIOC_KVREAD(((unsigned long)0x80000000|(unsigned long)0x40000000) | ((sizeof
(struct pvbus_req) & 0x1fff) << 16) | ((('V')) <<
8) | ((1)))
: cmd;
177 } else
178 usage();
179
180 /* Re-open read-writable */
181 if (cmd == PVBUSIOC_KVWRITE(((unsigned long)0x80000000|(unsigned long)0x40000000) | ((sizeof
(struct pvbus_req) & 0x1fff) << 16) | ((('V')) <<
8) | ((2)))
) {
182 close(fd);
183 if ((fd = open(path_pvbus, O_RDWR0x0002)) == -1)
184 err(1, "open: %s", path_pvbus);
185 }
186
187 if ((ret = ioctl(fd, cmd, &pvr, sizeof(pvr))) == -1)
188 err(1, "ioctl");
189
190 if (!qflag && strlen(pvr.pvr_value)) {
191 /*
192 * The value can contain newlines and basically anything;
193 * only encode the unsafe characters that could perform
194 * unexpected functions on the terminal.
195 */
196 if (stravis(&str, pvr.pvr_value, VIS_SAFE0x20 | VIS_CSTYLE0x02) == -1)
197 err(1, "stravis");
198 fprintf(outfp, "%s\n", str);
199 free(str);
200 }
201
202 done:
203 if (outfp != stdout(&__sF[1]))
204 fclose(outfp);
205 free(pvr.pvr_value);
206 free(pvr.pvr_key);
207 close(fd);
208
209 return (0);
210}