xref: /llvm-project/clang/test/Analysis/chroot.c (revision e034c4ef7b52635bb9cc78b6d3f97a4af5002f92)
1 // RUN: %clang_analyze_cc1 -analyzer-checker=unix.Chroot -analyzer-output=text -verify %s
2 
3 extern int chroot(const char* path);
4 extern int chdir(const char* path);
5 
6 void foo(void) {
7 }
8 
9 void 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 
16 void f2(void) {
17   chroot("/usr/local"); // root changed.
18   chdir("/"); // enter the jail.
19   foo(); // no-warning
20 }
21 
22 void 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 
30 void f4(void) {
31   if (chroot("/usr/local") == 0) {
32       chdir("../"); // change working directory, still out of jail.
33   }
34 }
35 
36 void f5(void) {
37   int v = chroot("/usr/local");
38   if (v == -1) {
39       foo();        // no warning, chroot failed
40       chdir("../"); // change working directory, still out of jail.
41   }
42 }
43 
44 void f6(void) {
45   if (chroot("/usr/local") == -1) {
46       chdir("../"); // change working directory, still out of jail.
47   }
48 }
49 
50 void 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 failed
54       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 
62 void 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