clang -cc1 -cc1 -triple amd64-unknown-openbsd7.4 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name chmod.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/bin/chmod/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/bin/chmod/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/bin/chmod/chmod.c
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | #include <sys/types.h> |
| 34 | #include <sys/stat.h> |
| 35 | |
| 36 | #include <err.h> |
| 37 | #include <errno.h> |
| 38 | #include <fcntl.h> |
| 39 | #include <fts.h> |
| 40 | #include <grp.h> |
| 41 | #include <limits.h> |
| 42 | #include <pwd.h> |
| 43 | #include <stdio.h> |
| 44 | #include <stdlib.h> |
| 45 | #include <string.h> |
| 46 | #include <unistd.h> |
| 47 | |
| 48 | int ischflags, ischown, ischgrp, ischmod; |
| 49 | extern char *__progname; |
| 50 | |
| 51 | gid_t a_gid(const char *); |
| 52 | uid_t a_uid(const char *, int); |
| 53 | static void __dead usage(void); |
| 54 | |
| 55 | int |
| 56 | main(int argc, char *argv[]) |
| 57 | { |
| 58 | FTS *ftsp; |
| 59 | FTSENT *p; |
| 60 | void *set; |
| 61 | unsigned long val; |
| 62 | int oct; |
| 63 | mode_t omode; |
| 1 | 'omode' declared without an initial value | |
|
| 64 | int Hflag, Lflag, Rflag, ch, fflag, fts_options, hflag, rval, atflags; |
| 65 | uid_t uid; |
| 66 | gid_t gid; |
| 67 | u_int32_t fclear, fset; |
| 68 | char *ep, *mode, *cp, *flags; |
| 69 | |
| 70 | if (strlen(__progname) > 2) { |
| 2 | | Assuming the condition is false | |
|
| |
| 71 | ischown = __progname[2] == 'o'; |
| 72 | ischgrp = __progname[2] == 'g'; |
| 73 | ischmod = __progname[2] == 'm'; |
| 74 | ischflags = __progname[2] == 'f'; |
| 75 | } |
| 76 | |
| 77 | uid = (uid_t)-1; |
| 78 | gid = (gid_t)-1; |
| 79 | Hflag = Lflag = Rflag = fflag = hflag = 0; |
| 80 | while ((ch = getopt(argc, argv, "HLPRXfghorstuwx")) != -1) |
| 4 | | Assuming the condition is true | |
|
| 5 | | Loop condition is true. Entering loop body | |
|
| 7 | | Execution continues on line 80 | |
|
| 8 | | Assuming the condition is false | |
|
| 9 | | Loop condition is false. Execution continues on line 125 | |
|
| 81 | switch (ch) { |
| 6 | | Control jumps to 'case 104:' at line 99 | |
|
| 82 | case 'H': |
| 83 | Hflag = 1; |
| 84 | Lflag = 0; |
| 85 | break; |
| 86 | case 'L': |
| 87 | Lflag = 1; |
| 88 | Hflag = 0; |
| 89 | break; |
| 90 | case 'P': |
| 91 | Hflag = Lflag = 0; |
| 92 | break; |
| 93 | case 'R': |
| 94 | Rflag = 1; |
| 95 | break; |
| 96 | case 'f': |
| 97 | fflag = 1; |
| 98 | break; |
| 99 | case 'h': |
| 100 | hflag = 1; |
| 101 | break; |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | case 'g': case 'o': case 'r': case 's': |
| 107 | case 't': case 'u': case 'w': case 'X': case 'x': |
| 108 | if (!ischmod) |
| 109 | usage(); |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | if (optind > 1) { |
| 116 | cp = argv[optind - 1]; |
| 117 | if (cp[strlen(cp) - 1] == ch) |
| 118 | --optind; |
| 119 | } |
| 120 | goto done; |
| 121 | default: |
| 122 | usage(); |
| 123 | } |
| 124 | done: |
| 125 | argv += optind; |
| 126 | argc -= optind; |
| 127 | |
| 128 | if (argc < 2) |
| 10 | | Assuming 'argc' is >= 2 | |
|
| |
| 129 | usage(); |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | atflags = AT_SYMLINK_NOFOLLOW; |
| 136 | |
| 137 | fts_options = FTS_PHYSICAL; |
| 138 | if (Rflag) { |
| |
| 139 | if (hflag) |
| 140 | errx(1, |
| 141 | "the -R and -h options may not be specified together."); |
| 142 | if (Hflag) |
| 143 | fts_options |= FTS_COMFOLLOW; |
| 144 | if (Lflag) { |
| 145 | fts_options &= ~FTS_PHYSICAL; |
| 146 | fts_options |= FTS_LOGICAL; |
| 147 | atflags = 0; |
| 148 | } |
| 149 | } else if (!hflag) { |
| |
| 150 | fts_options |= FTS_COMFOLLOW; |
| 151 | atflags = 0; |
| 152 | } |
| 153 | |
| 154 | if (ischflags) { |
| 14 | | Assuming 'ischflags' is not equal to 0 | |
|
| |
| 155 | if (pledge("stdio rpath fattr", NULL) == -1) |
| 16 | | Assuming the condition is false | |
|
| |
| 156 | err(1, "pledge"); |
| 157 | |
| 158 | flags = *argv; |
| 159 | if (*flags >= '0' && *flags <= '7') { |
| 18 | | Assuming the condition is true | |
|
| 19 | | Assuming the condition is true | |
|
| |
| 160 | errno = 0; |
| 161 | val = strtoul(flags, &ep, 8); |
| 162 | if (val > UINT_MAX) |
| 21 | | Assuming 'val' is <= UINT_MAX | |
|
| |
| 163 | errno = ERANGE; |
| 164 | if (errno) |
| 23 | | Assuming the condition is false | |
|
| |
| 165 | err(1, "invalid flags: %s", flags); |
| 166 | if (*ep) |
| 25 | | Assuming the condition is false | |
|
| |
| 167 | errx(1, "invalid flags: %s", flags); |
| 168 | fset = val; |
| 169 | oct = 1; |
| 170 | } else { |
| 171 | if (strtofflags(&flags, &fset, &fclear)) |
| 172 | errx(1, "invalid flag: %s", flags); |
| 173 | fclear = ~fclear; |
| 174 | oct = 0; |
| 175 | } |
| 176 | } else if (ischmod) { |
| 177 | mode = *argv; |
| 178 | if (*mode >= '0' && *mode <= '7') { |
| 179 | errno = 0; |
| 180 | val = strtoul(mode, &ep, 8); |
| 181 | if (val > INT_MAX) |
| 182 | errno = ERANGE; |
| 183 | if (errno) |
| 184 | err(1, "invalid file mode: %s", mode); |
| 185 | if (*ep) |
| 186 | errx(1, "invalid file mode: %s", mode); |
| 187 | omode = val; |
| 188 | oct = 1; |
| 189 | } else { |
| 190 | if ((set = setmode(mode)) == NULL) |
| 191 | errx(1, "invalid file mode: %s", mode); |
| 192 | oct = 0; |
| 193 | } |
| 194 | } else if (ischown) { |
| 195 | |
| 196 | if ((cp = strchr(*argv, ':')) != NULL) { |
| 197 | *cp++ = '\0'; |
| 198 | gid = a_gid(cp); |
| 199 | } |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | else if ((cp = strchr(*argv, '.')) != NULL && |
| 206 | (uid = a_uid(*argv, 1)) == (uid_t)-1) { |
| 207 | *cp++ = '\0'; |
| 208 | gid = a_gid(cp); |
| 209 | } |
| 210 | if (uid == (uid_t)-1) |
| 211 | uid = a_uid(*argv, 0); |
| 212 | } else |
| 213 | gid = a_gid(*argv); |
| 214 | |
| 215 | if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL) |
| 27 | | Assuming the condition is false | |
|
| |
| 216 | err(1, NULL); |
| 217 | for (rval = 0; (p = fts_read(ftsp)) != NULL;) { |
| 29 | | Assuming the condition is true | |
|
| 30 | | Loop condition is true. Entering loop body | |
|
| 218 | switch (p->fts_info) { |
| 31 | | Control jumps to the 'default' case at line 250 | |
|
| 219 | case FTS_D: |
| 220 | if (!Rflag) |
| 221 | fts_set(ftsp, p, FTS_SKIP); |
| 222 | if (ischmod) |
| 223 | break; |
| 224 | else |
| 225 | continue; |
| 226 | case FTS_DNR: |
| 227 | warnc(p->fts_errno, "%s", p->fts_path); |
| 228 | rval = 1; |
| 229 | break; |
| 230 | case FTS_DP: |
| 231 | if (ischmod) |
| 232 | continue; |
| 233 | else |
| 234 | break; |
| 235 | case FTS_ERR: |
| 236 | case FTS_NS: |
| 237 | warnc(p->fts_errno, "%s", p->fts_path); |
| 238 | rval = 1; |
| 239 | continue; |
| 240 | case FTS_SL: |
| 241 | case FTS_SLNONE: |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | if (!hflag && (fts_options & FTS_LOGICAL)) |
| 248 | continue; |
| 249 | break; |
| 250 | default: |
| 251 | break; |
| 252 | } |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | if (fts_options & FTS_COMFOLLOW) { |
| |
| 259 | atflags = p->fts_level == FTS_ROOTLEVEL ? 0 : |
| 260 | AT_SYMLINK_NOFOLLOW; |
| 261 | } |
| 262 | |
| 263 | if (ischmod) { |
| 32 | | Execution continues on line 258 | |
|
| 34 | | Assuming 'ischmod' is not equal to 0 | |
|
| 264 | if (!fchmodat(AT_FDCWD, p->fts_accpath, oct ? omode : |
| |
| 36 | | 3rd function call argument is an uninitialized value |
|
| 265 | getmode(set, p->fts_statp->st_mode), atflags) |
| 266 | || fflag) |
| 267 | continue; |
| 268 | } else if (!ischflags) { |
| 269 | if (!fchownat(AT_FDCWD, p->fts_accpath, uid, gid, |
| 270 | atflags) || fflag) |
| 271 | continue; |
| 272 | } else { |
| 273 | if (!chflagsat(AT_FDCWD, p->fts_accpath, oct ? fset : |
| 274 | (p->fts_statp->st_flags | fset) & fclear, atflags)) |
| 275 | continue; |
| 276 | } |
| 277 | |
| 278 | |
| 279 | warn("%s", p->fts_path); |
| 280 | rval = 1; |
| 281 | } |
| 282 | if (errno) |
| 283 | err(1, "fts_read"); |
| 284 | fts_close(ftsp); |
| 285 | return (rval); |
| 286 | } |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | uid_t |
| 294 | a_uid(const char *s, int silent) |
| 295 | { |
| 296 | const char *errstr; |
| 297 | uid_t uid; |
| 298 | |
| 299 | if (*s == '\0') |
| 300 | return ((uid_t)-1); |
| 301 | |
| 302 | |
| 303 | if (uid_from_user(s, &uid) != -1) |
| 304 | return (uid); |
| 305 | |
| 306 | |
| 307 | uid = (uid_t)strtonum(s, 0, UID_MAX, &errstr); |
| 308 | if (errstr) { |
| 309 | if (silent) |
| 310 | return ((uid_t)-1); |
| 311 | else |
| 312 | errx(1, "user is %s: %s", errstr, s); |
| 313 | } |
| 314 | |
| 315 | return (uid); |
| 316 | } |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | gid_t |
| 323 | a_gid(const char *s) |
| 324 | { |
| 325 | const char *errstr; |
| 326 | gid_t gid; |
| 327 | |
| 328 | if (*s == '\0') |
| 329 | return ((gid_t)-1); |
| 330 | |
| 331 | |
| 332 | if (gid_from_group(s, &gid) != -1) |
| 333 | return (gid); |
| 334 | |
| 335 | |
| 336 | gid = (gid_t)strtonum(s, 0, GID_MAX, &errstr); |
| 337 | if (errstr) |
| 338 | errx(1, "group is %s: %s", errstr, s); |
| 339 | |
| 340 | return (gid); |
| 341 | } |
| 342 | |
| 343 | static void __dead |
| 344 | usage(void) |
| 345 | { |
| 346 | fprintf(stderr, |
| 347 | "usage: %s [-h] [-R [-H | -L | -P]] %s file ...\n", |
| 348 | __progname, ischmod ? "mode" : ischflags ? "flags" : |
| 349 | ischown ? "owner[:group]" : "group"); |
| 350 | if (ischown) |
| 351 | fprintf(stderr, |
| 352 | " %s [-h] [-R [-H | -L | -P]] :group file ...\n", |
| 353 | __progname); |
| 354 | exit(1); |
| 355 | } |