Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
  • Loading branch information
netblue30 committed Nov 17, 2016
1 parent f233eee commit cee0a4d
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 40 deletions.
19 changes: 8 additions & 11 deletions src/fcopy/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,22 +188,19 @@ static int fs_copydir(const char *infname, const struct stat *st, int ftype, str
static char *check(const char *src) {
struct stat s;
char *rsrc = realpath(src, NULL);
if (!rsrc || stat(rsrc, &s) == -1) {
fprintf(stderr, "Error fcopy: cannot find %s directory\n", src);
exit(1);
}
if (!rsrc || stat(rsrc, &s) == -1)
goto errexit;

// check uid
if (s.st_uid != getuid() || s.st_gid != getgid()) {
fprintf(stderr, "Error fcopy: uid/gid mismatch for %s\n", rsrc);
exit(1);
}
if (s.st_uid != getuid() || s.st_gid != getgid())
goto errexit;

// dir, link, regular file
if (S_ISDIR(s.st_mode) || S_ISREG(s.st_mode) || S_ISLNK(s.st_mode)) {
if (S_ISDIR(s.st_mode) || S_ISREG(s.st_mode) || S_ISLNK(s.st_mode))
return rsrc; // normal exit from the function
}
fprintf(stderr, "Error fcopy: invalid directory %s\n", rsrc);

errexit:
fprintf(stderr, "Error fcopy: invalid file %s\n", src);
exit(1);
}

Expand Down
1 change: 0 additions & 1 deletion src/firemon/caps.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ static void print_caps(int pid) {
char *file;
if (asprintf(&file, "/proc/%d/status", pid) == -1) {
errExit("asprintf");
exit(1);
}

FILE *fp = fopen(file, "r");
Expand Down
6 changes: 2 additions & 4 deletions src/firemon/procevent.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ static int pid_is_firejail(pid_t pid) {

// open /proc/self/comm
char *file;
if (asprintf(&file, "/proc/%u/comm", pid) == -1) {
perror("asprintf");
exit(1);
}
if (asprintf(&file, "/proc/%u/comm", pid) == -1)
errExit("asprintf");

FILE *fp = fopen(file, "r");
if (!fp) {
Expand Down
4 changes: 1 addition & 3 deletions src/firemon/seccomp.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@
#define MAXBUF 4098
static void print_seccomp(int pid) {
char *file;
if (asprintf(&file, "/proc/%d/status", pid) == -1) {
if (asprintf(&file, "/proc/%d/status", pid) == -1)
errExit("asprintf");
exit(1);
}

FILE *fp = fopen(file, "r");
if (!fp) {
Expand Down
32 changes: 12 additions & 20 deletions src/ftee/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,6 @@ static int is_link(const char *fname) {
return 0;
}





static void usage(void) {
printf("Usage: ftee filename\n");
}
Expand All @@ -201,33 +197,25 @@ int main(int argc, char **argv) {


// do not accept directories, links, and files with ".."
if (strstr(fname, "..") || is_link(fname) || is_dir(fname)) {
fprintf(stderr, "Error: invalid output file. Links, directories and files with \"..\" are not allowed.\n");
exit(1);
}
if (strstr(fname, "..") || is_link(fname) || is_dir(fname))
goto errexit;

struct stat s;
if (stat(fname, &s) == 0) {
// check permissions
if (s.st_uid != getuid() || s.st_gid != getgid()) {
fprintf(stderr, "Error: the output file needs to be owned by the current user.\n");
exit(1);
}
if (s.st_uid != getuid() || s.st_gid != getgid())
goto errexit;

// check hard links
if (s.st_nlink != 1) {
fprintf(stderr, "Error: no hard links allowed.\n");
exit(1);
}
if (s.st_nlink != 1)
goto errexit;
}

// check if we can append to this file
/* coverity[toctou] */
FILE *fp = fopen(fname, "a");
if (!fp) {
fprintf(stderr, "Error: cannot open output file %s\n", fname);
exit(1);
}
if (!fp)
goto errexit;
fclose(fp);


Expand All @@ -248,4 +236,8 @@ int main(int argc, char **argv) {

log_close();
return 0;

errexit:
fprintf(stderr, "Error ftee: invalid output file.\n");
return 1;
}
1 change: 0 additions & 1 deletion test/utils/firemon-cpu.exp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ sleep 1

spawn $env(SHELL)
send -- "firemon --cpu\r"
sleep 4
expect {
timeout {puts "TESTING ERROR 2\n";exit}
"need to be root" {puts "TESTING SKIP: /proc mounted as hidepid\n"; exit}
Expand Down
18 changes: 18 additions & 0 deletions test/utils/firemon-interface.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/expect -f
# This file is part of Firejail project
# Copyright (C) 2014-2016 Firejail Authors
# License GPL v2

set timeout 10
spawn $env(SHELL)
match_max 100000

send -- "firemon --interface\r"
expect {
timeout {puts "TESTING ERROR 0\n";exit}
"you need to be root"
}
after 100

puts "\nall done\n"

28 changes: 28 additions & 0 deletions test/utils/firemon-name.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/expect -f
# This file is part of Firejail project
# Copyright (C) 2014-2016 Firejail Authors
# License GPL v2

set timeout 10
spawn $env(SHELL)
match_max 100000

send -- "firejail --name=test\r"
expect {
timeout {puts "TESTING ERROR 0\n";exit}
"Child process initialized"
}
sleep 1

spawn $env(SHELL)
send -- "firemon --cpu --name=test\r"
expect {
timeout {puts "TESTING ERROR 2\n";exit}
"need to be root" {puts "TESTING SKIP: /proc mounted as hidepid\n"; exit}
"Cpus_allowed_list"
}

after 100

puts "\nall done\n"

18 changes: 18 additions & 0 deletions test/utils/firemon-version.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/expect -f
# This file is part of Firejail project
# Copyright (C) 2014-2016 Firejail Authors
# License GPL v2

set timeout 10
spawn $env(SHELL)
match_max 100000

send -- "firemon --version\r"
expect {
timeout {puts "TESTING ERROR 0\n";exit}
"firemon version"
}
after 100

puts "\nall done\n"

9 changes: 9 additions & 0 deletions test/utils/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,12 @@ echo "TESTING: firemon cpu (test/utils/firemon-cpu.exp)"
echo "TESTING: firemon cgroup (test/utils/firemon-cgroup.exp)"
./firemon-cgroup.exp

echo "TESTING: firemon version (test/utils/firemon-version.exp)"
./firemon-version.exp

echo "TESTING: firemon interface (test/utils/firemon-interface.exp)"
./firemon-interface.exp

echo "TESTING: firemon name (test/utils/firemon-name.exp)"
./firemon-name.exp

0 comments on commit cee0a4d

Please sign in to comment.