69 lines · c
1// RUN: %clang_analyze_cc1 -analyzer-checker=unix.Chroot -analyzer-output=text -verify %s2 3extern int chroot(const char* path);4extern int chdir(const char* path);5 6void foo(void) {7}8 9void f1(void) {10 chroot("/usr/local"); // expected-note {{chroot called here}}11 foo();12 // expected-warning@-1 {{No call of chdir("/") immediately after chroot}}13 // expected-note@-2 {{No call of chdir("/") immediately after chroot}}14}15 16void f2(void) {17 chroot("/usr/local"); // root changed.18 chdir("/"); // enter the jail.19 foo(); // no-warning20}21 22void f3(void) {23 chroot("/usr/local"); // expected-note {{chroot called here}}24 chdir("../"); // change working directory, still out of jail.25 foo();26 // expected-warning@-1 {{No call of chdir("/") immediately after chroot}}27 // expected-note@-2 {{No call of chdir("/") immediately after chroot}}28}29 30void f4(void) {31 if (chroot("/usr/local") == 0) {32 chdir("../"); // change working directory, still out of jail.33 }34}35 36void f5(void) {37 int v = chroot("/usr/local");38 if (v == -1) {39 foo(); // no warning, chroot failed40 chdir("../"); // change working directory, still out of jail.41 }42}43 44void f6(void) {45 if (chroot("/usr/local") == -1) {46 chdir("../"); // change working directory, still out of jail.47 }48}49 50void f7(void) {51 int v = chroot("/usr/local"); // expected-note {{chroot called here}}52 if (v == -1) { // expected-note {{Taking false branch}}53 foo(); // no warning, chroot failed54 chdir("../"); // change working directory, still out of jail.55 } else {56 foo();57 // expected-warning@-1 {{No call of chdir("/") immediately after chroot}}58 // expected-note@-2 {{No call of chdir("/") immediately after chroot}}59 }60}61 62void f8() {63 chroot("/usr/local"); // expected-note {{chroot called here}}64 chdir("/usr"); // This chdir was ineffective because it's not exactly `chdir("/")`.65 foo();66 // expected-warning@-1 {{No call of chdir("/") immediately after chroot}}67 // expected-note@-2 {{No call of chdir("/") immediately after chroot}}68}69