clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name registry.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 2 -fhalf-no-semantic-interposition -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/__w/nvme-cli/nvme-cli/.build-ci -fcoverage-compilation-dir=/__w/nvme-cli/nvme-cli/.build-ci -resource-dir /usr/lib/llvm-19/lib/clang/19 -include /__w/nvme-cli/nvme-cli/.build-ci/nvme-config.h -I libnvme/src/libnvme3.so.3.0.0.p -I libnvme/src -I ../libnvme/src -I ccan -I ../ccan -I . -I .. -I /usr/include/json-c -D _FILE_OFFSET_BITS=64 -D _GNU_SOURCE -U NDEBUG -internal-isystem /usr/lib/llvm-19/lib/clang/19/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -std=gnu99 -ferror-limit 19 -fvisibility=hidden -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-opt-analyze-headers -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /__w/nvme-cli/nvme-cli/.build-ci/scan-results/2026-06-24-175442-590-1 -x c ../libnvme/src/nvme/registry.c
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | #include <dirent.h> |
| 10 | #include <errno.h> |
| 11 | #include <fcntl.h> |
| 12 | #include <limits.h> |
| 13 | #include <stdbool.h> |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
| 17 | #include <sys/stat.h> |
| 18 | #include <sys/types.h> |
| 19 | #include <unistd.h> |
| 20 | |
| 21 | #include "cleanup.h" |
| 22 | #include "compiler-attributes.h" |
| 23 | #include "private.h" |
| 24 | #include "registry.h" |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | static const char *registry_dir(void) |
| 33 | { |
| 34 | static char buf[256]; |
| 35 | static const char *dir; |
| 36 | |
| 37 | if (!dir) { |
| 38 | const char *env = getenv("NVME_REGISTRY_DIR"); |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | if (env && strncmp(env, "/tmp/", 5) == 0 && |
| 54 | !strstr(env, "..")) { |
| 55 | snprintf(buf, sizeof(buf), "%s", env); |
| 56 | dir = buf; |
| 57 | } else { |
| 58 | dir = "/run/nvme/registry"; |
| 59 | } |
| 60 | } |
| 61 | return dir; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | static char *registry_path(const char *device, const char *attr) |
| 70 | { |
| 71 | char *path = NULL; |
| 72 | int n; |
| 73 | |
| 74 | if (attr) |
| |
| 75 | n = asprintf(&path, "%s/%s/%s", registry_dir(), device, attr); |
| 76 | else |
| 77 | n = asprintf(&path, "%s/%s", registry_dir(), device); |
| 78 | return n < 0 ? NULL : path; |
| |
| |
| 9 | | Returning pointer, which participates in a condition later | |
|
| 79 | } |
| 80 | |
| 81 | |
| 82 | static int mkdir_p(const char *path, mode_t mode) |
| 83 | { |
| 84 | __cleanup_free char *tmp = strdup(path); |
| 85 | size_t len; |
| 86 | char *p; |
| 87 | |
| 88 | if (!tmp) |
| 89 | return -ENOMEM; |
| 90 | len = strlen(tmp); |
| 91 | if (len && tmp[len - 1] == '/') |
| 92 | tmp[len - 1] = '\0'; |
| 93 | |
| 94 | for (p = tmp + 1; *p; p++) { |
| 95 | if (*p != '/') |
| 96 | continue; |
| 97 | *p = '\0'; |
| 98 | if (mkdir(tmp, mode) < 0 && errno != EEXIST) |
| 99 | return -errno; |
| 100 | *p = '/'; |
| 101 | } |
| 102 | if (mkdir(tmp, mode) < 0 && errno != EEXIST) |
| 103 | return -errno; |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static int ensure_device_dir(const char *device) |
| 108 | { |
| 109 | __cleanup_free char *path = NULL; |
| 110 | int ret; |
| 111 | |
| 112 | ret = mkdir_p(registry_dir(), 0755); |
| 113 | if (ret) |
| 114 | return ret; |
| 115 | |
| 116 | path = registry_path(device, NULL); |
| 117 | if (!path) |
| 118 | return -ENOMEM; |
| 119 | |
| 120 | |
| 121 | if (mkdir(path, 0755) < 0 && errno != EEXIST) |
| 122 | return -errno; |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static bool valid_device(const char *device) |
| 127 | { |
| 128 | const char *p; |
| 129 | |
| 130 | if (!device || strncmp(device, "nvme", 4) != 0) |
| 131 | return false; |
| 132 | p = device + 4; |
| 133 | if (!*p) |
| 134 | return false; |
| 135 | for (; *p; p++) { |
| 136 | if (*p < '0' || *p > '9') |
| 137 | return false; |
| 138 | } |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | static bool valid_attr(const char *attr) |
| 143 | { |
| 144 | const char *p; |
| 145 | |
| 146 | if (!attr || !*attr) |
| 147 | return false; |
| 148 | for (p = attr; *p; p++) { |
| 149 | if ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') || |
| 150 | (*p >= '0' && *p <= '9') || *p == '_' || *p == '-') |
| 151 | continue; |
| 152 | return false; |
| 153 | } |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | static int write_all(int fd, const char *buf, size_t len) |
| 158 | { |
| 159 | while (len) { |
| 160 | ssize_t w = write(fd, buf, len); |
| 161 | |
| 162 | if (w < 0) { |
| 163 | if (errno == EINTR) |
| 164 | continue; |
| 165 | return -errno; |
| 166 | } |
| 167 | if (w == 0) |
| 168 | return -EIO; |
| 169 | buf += w; |
| 170 | len -= w; |
| 171 | } |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | static ssize_t read_full(int fd, char *buf, size_t count) |
| 180 | { |
| 181 | size_t off = 0; |
| 182 | |
| 183 | while (off < count) { |
| 184 | ssize_t r = read(fd, buf + off, count - off); |
| 185 | |
| 186 | if (r < 0) { |
| 187 | if (errno == EINTR) |
| 188 | continue; |
| 189 | return -errno; |
| 190 | } |
| 191 | if (r == 0) |
| 192 | break; |
| 193 | off += r; |
| 194 | } |
| 195 | return off; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | static int read_attr_fd(int fd, char **out) |
| 205 | { |
| 206 | struct stat st; |
| 207 | char *val; |
| 208 | ssize_t n; |
| 209 | |
| 210 | if (fstat(fd, &st) < 0) |
| 211 | return -errno; |
| 212 | if (st.st_size == 0) |
| 213 | return -ENOENT; |
| 214 | |
| 215 | val = malloc(st.st_size + 1); |
| 216 | if (!val) |
| 217 | return -ENOMEM; |
| 218 | |
| 219 | n = read_full(fd, val, st.st_size); |
| 220 | if (n < 0) { |
| 221 | free(val); |
| 222 | return n; |
| 223 | } |
| 224 | |
| 225 | while (n > 0 && (val[n - 1] == '\n' || val[n - 1] == '\r')) |
| 226 | n--; |
| 227 | val[n] = '\0'; |
| 228 | *out = val; |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | static int write_attr_atomic(int dir_fd, const char *dir_path, |
| 248 | const char *attr, const char *value) |
| 249 | { |
| 250 | __cleanup_free char *tmp_path = NULL; |
| 251 | __cleanup_free char *final_path = NULL; |
| 252 | int fd, ret; |
| 253 | |
| 254 | if (!valid_attr(attr)) |
| 255 | return -EINVAL; |
| 256 | |
| 257 | if (!value) { |
| 258 | if (unlinkat(dir_fd, attr, 0) < 0 && errno != ENOENT) |
| 259 | return -errno; |
| 260 | fsync(dir_fd); |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | if (asprintf(&final_path, "%s/%s", dir_path, attr) < 0) |
| 265 | return -ENOMEM; |
| 266 | if (asprintf(&tmp_path, "%s/%s.tmp.XXXXXX", dir_path, attr) < 0) |
| 267 | return -ENOMEM; |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | #ifdef _GNU_SOURCE |
| 275 | fd = mkostemp(tmp_path, O_CLOEXEC); |
| 276 | if (fd < 0) |
| 277 | return -errno; |
| 278 | #else |
| 279 | fd = mkstemp(tmp_path); |
| 280 | if (fd < 0) |
| 281 | return -errno; |
| 282 | if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) { |
| 283 | ret = -errno; |
| 284 | goto err; |
| 285 | } |
| 286 | #endif |
| 287 | |
| 288 | |
| 289 | if (fchmod(fd, 0644) < 0) { |
| 290 | ret = -errno; |
| 291 | goto err; |
| 292 | } |
| 293 | |
| 294 | ret = write_all(fd, value, strlen(value)); |
| 295 | if (ret) |
| 296 | goto err; |
| 297 | ret = write_all(fd, "\n", 1); |
| 298 | if (ret) |
| 299 | goto err; |
| 300 | close(fd); |
| 301 | |
| 302 | if (rename(tmp_path, final_path) < 0) { |
| 303 | ret = -errno; |
| 304 | unlink(tmp_path); |
| 305 | return ret; |
| 306 | } |
| 307 | |
| 308 | |
| 309 | fsync(dir_fd); |
| 310 | return 0; |
| 311 | |
| 312 | err: |
| 313 | close(fd); |
| 314 | unlink(tmp_path); |
| 315 | return ret; |
| 316 | } |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | static int write_device_attr(const char *device, const char *attr, |
| 323 | const char *value) |
| 324 | { |
| 325 | __cleanup_free char *dir_path = NULL; |
| 326 | int dir_fd, ret; |
| 327 | |
| 328 | ret = ensure_device_dir(device); |
| 329 | if (ret) |
| 330 | return ret; |
| 331 | |
| 332 | dir_path = registry_path(device, NULL); |
| 333 | if (!dir_path) |
| 334 | return -ENOMEM; |
| 335 | |
| 336 | dir_fd = open(dir_path, O_RDONLY | O_DIRECTORY | O_CLOEXEC); |
| 337 | if (dir_fd < 0) |
| 338 | return -errno; |
| 339 | |
| 340 | ret = write_attr_atomic(dir_fd, dir_path, attr, value); |
| 341 | close(dir_fd); |
| 342 | return ret; |
| 343 | } |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | int libnvmf_registry_create_instance(struct libnvme_global_ctx *ctx, |
| 353 | int instance, const char *owner) |
| 354 | { |
| 355 | char device[32]; |
| 356 | |
| 357 | snprintf(device, sizeof(device), "nvme%d", instance); |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | libnvmf_registry_delete(ctx, device); |
| 367 | |
| 368 | return write_device_attr(device, "owner", owner); |
| 369 | } |
| 370 | |
| 371 | int libnvmf_registry_delete_instance(struct libnvme_global_ctx *ctx, |
| 372 | int instance) |
| 373 | { |
| 374 | char device[32]; |
| 375 | int ret; |
| 376 | |
| 377 | snprintf(device, sizeof(device), "nvme%d", instance); |
| 378 | ret = libnvmf_registry_delete(ctx, device); |
| 1 | Calling 'libnvmf_registry_delete' | |
|
| 379 | return (ret == -ENOENT) ? 0 : ret; |
| 380 | } |
| 381 | |
| 382 | __libnvme_public int libnvmf_registry_retrieve(struct libnvme_global_ctx *ctx, |
| 383 | const char *device, |
| 384 | const char *attr, char **value) |
| 385 | { |
| 386 | __cleanup_free char *path = NULL; |
| 387 | int fd, ret; |
| 388 | |
| 389 | if (!ctx) |
| 390 | return -EINVAL; |
| 391 | if (!device || !attr || !value) |
| 392 | return -EINVAL; |
| 393 | if (!valid_device(device)) |
| 394 | return -EINVAL; |
| 395 | |
| 396 | path = registry_path(device, attr); |
| 397 | if (!path) |
| 398 | return -ENOMEM; |
| 399 | |
| 400 | fd = open(path, O_RDONLY | O_CLOEXEC); |
| 401 | if (fd < 0) |
| 402 | return -errno; |
| 403 | |
| 404 | ret = read_attr_fd(fd, value); |
| 405 | close(fd); |
| 406 | return ret; |
| 407 | } |
| 408 | |
| 409 | __libnvme_public int libnvmf_registry_attr_equal(struct libnvme_global_ctx *ctx, |
| 410 | const char *device, |
| 411 | const char *attr, |
| 412 | const char *value) |
| 413 | { |
| 414 | char *stored = NULL; |
| 415 | int rc; |
| 416 | |
| 417 | if (!ctx) |
| 418 | return -EINVAL; |
| 419 | |
| 420 | rc = libnvmf_registry_retrieve(ctx, device, attr, &stored); |
| 421 | if (rc < 0 && rc != -ENOENT) |
| 422 | return rc; |
| 423 | if (!stored) |
| 424 | rc = value ? 1 : 0; |
| 425 | else |
| 426 | rc = (value && !strcmp(stored, value)) ? 0 : 1; |
| 427 | free(stored); |
| 428 | return rc; |
| 429 | } |
| 430 | |
| 431 | __libnvme_public int libnvmf_registry_update(struct libnvme_global_ctx *ctx, |
| 432 | const char *device, |
| 433 | const char *attr, const char *value) |
| 434 | { |
| 435 | if (!ctx) |
| 436 | return -EINVAL; |
| 437 | if (!device || !valid_device(device)) |
| 438 | return -EINVAL; |
| 439 | |
| 440 | return write_device_attr(device, attr, value); |
| 441 | } |
| 442 | |
| 443 | __libnvme_public int libnvmf_registry_delete(struct libnvme_global_ctx *ctx, |
| 444 | const char *device) |
| 445 | { |
| 446 | __cleanup_free char *path = NULL; |
| 447 | struct dirent *de; |
| 448 | int dir_fd, ret = 0; |
| 449 | DIR *d; |
| 450 | |
| 451 | if (!ctx) |
| 2 | | Assuming 'ctx' is non-null | |
|
| 452 | return -EINVAL; |
| 453 | if (!device || !valid_device(device)) |
| 3 | | Assuming the condition is false | |
|
| |
| 454 | return -EINVAL; |
| 455 | |
| 456 | path = registry_path(device, NULL); |
| |
| 10 | | Returning from 'registry_path' | |
|
| 457 | if (!path) |
| 11 | | Assuming 'path' is non-null | |
|
| |
| 458 | return -ENOMEM; |
| 459 | |
| 460 | d = opendir(path); |
| 13 | | Assuming that 'opendir' is successful | |
|
| 461 | if (!d) |
| |
| 462 | return -errno; |
| 463 | |
| 464 | dir_fd = dirfd(d); |
| 15 | | Assuming that 'dirfd' fails | |
|
| 16 | | Value assigned to 'dir_fd' | |
|
| 465 | while ((de = readdir(d)) != NULL) { |
| 17 | | Assuming the condition is true | |
|
| 18 | | Loop condition is true. Entering loop body | |
|
| 466 | if (de->d_name[0] == '.') |
| 19 | | Assuming the condition is false | |
|
| 467 | continue; |
| 468 | if (unlinkat(dir_fd, de->d_name, 0) < 0 && errno != ENOENT) |
| 20 | | The 1st argument to 'unlinkat' is -1 but should be a valid file descriptor or AT_FDCWD |
|
| 469 | ret = -errno; |
| 470 | } |
| 471 | closedir(d); |
| 472 | |
| 473 | if (ret) |
| 474 | return ret; |
| 475 | |
| 476 | if (rmdir(path) < 0 && errno != ENOENT) |
| 477 | return -errno; |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | __libnvme_public int libnvmf_registry_device_for_each( |
| 482 | struct libnvme_global_ctx *ctx, |
| 483 | void (*callback)(const char *device, void *user_data), |
| 484 | void *user_data) |
| 485 | { |
| 486 | char dev_path[NAME_MAX + 6]; |
| 487 | struct dirent *de; |
| 488 | DIR *d; |
| 489 | |
| 490 | if (!ctx) |
| 491 | return -EINVAL; |
| 492 | if (!callback) |
| 493 | return -EINVAL; |
| 494 | |
| 495 | d = opendir(registry_dir()); |
| 496 | if (!d) { |
| 497 | if (errno == ENOENT) |
| 498 | return 0; |
| 499 | return -errno; |
| 500 | } |
| 501 | |
| 502 | while ((de = readdir(d)) != NULL) { |
| 503 | if (de->d_name[0] == '.') |
| 504 | continue; |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | if (de->d_type != DT_DIR) { |
| 511 | struct stat st; |
| 512 | |
| 513 | if (de->d_type != DT_UNKNOWN) |
| 514 | continue; |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | if (fstatat(dirfd(d), de->d_name, &st, 0) < 0 || |
| 521 | !S_ISDIR(st.st_mode)) |
| 522 | continue; |
| 523 | } |
| 524 | |
| 525 | |
| 526 | snprintf(dev_path, sizeof(dev_path), "/dev/%s", de->d_name); |
| 527 | if (access(dev_path, F_OK) != 0) |
| 528 | continue; |
| 529 | |
| 530 | callback(de->d_name, user_data); |
| 531 | } |
| 532 | closedir(d); |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | __libnvme_public int libnvmf_registry_attr_for_each( |
| 537 | struct libnvme_global_ctx *ctx, |
| 538 | const char *device, |
| 539 | void (*callback)(const char *attr, const char *value, |
| 540 | void *user_data), |
| 541 | void *user_data) |
| 542 | { |
| 543 | __cleanup_free char *dir_path = NULL; |
| 544 | struct dirent *de; |
| 545 | int dir_fd; |
| 546 | DIR *d; |
| 547 | |
| 548 | if (!ctx) |
| 549 | return -EINVAL; |
| 550 | if (!device || !callback) |
| 551 | return -EINVAL; |
| 552 | if (!valid_device(device)) |
| 553 | return -EINVAL; |
| 554 | |
| 555 | dir_path = registry_path(device, NULL); |
| 556 | if (!dir_path) |
| 557 | return -ENOMEM; |
| 558 | |
| 559 | d = opendir(dir_path); |
| 560 | if (!d) |
| 561 | return -errno; |
| 562 | |
| 563 | dir_fd = dirfd(d); |
| 564 | while ((de = readdir(d)) != NULL) { |
| 565 | __cleanup_free char *val = NULL; |
| 566 | int fd, rc; |
| 567 | |
| 568 | if (de->d_name[0] == '.') |
| 569 | continue; |
| 570 | |
| 571 | if (strstr(de->d_name, ".tmp.")) |
| 572 | continue; |
| 573 | |
| 574 | fd = openat(dir_fd, de->d_name, O_RDONLY | O_CLOEXEC); |
| 575 | if (fd < 0) |
| 576 | continue; |
| 577 | |
| 578 | rc = read_attr_fd(fd, &val); |
| 579 | close(fd); |
| 580 | if (rc == 0) |
| 581 | callback(de->d_name, val, user_data); |
| 582 | } |
| 583 | closedir(d); |
| 584 | return 0; |
| 585 | } |