Skip to content

Commit bc39cb5

Browse files
committed
Fix resource leaks
Signed-off-by: Ilya Pankratov <[email protected]>
1 parent 53ad8d7 commit bc39cb5

File tree

11 files changed

+29
-4
lines changed

11 files changed

+29
-4
lines changed

src/fids/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ static void file_checksum(const char *fname) {
106106
}
107107
else {
108108
content = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
109-
close(fd);
110109
mmapped = 1;
111110
}
111+
close(fd);
112112

113113
unsigned char checksum[KEY_SIZE / 8];
114114
blake2b(checksum, sizeof(checksum), content, size);

src/firecfg/desktop_files.c

+3
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ void fix_desktop_files(const char *homedir) {
300300

301301
if (stat(outname, &sb) == 0) {
302302
printf(" %s skipped: file exists\n", filename);
303+
free(outname);
303304
if (change_exec)
304305
free(change_exec);
305306
continue;
@@ -308,6 +309,7 @@ void fix_desktop_files(const char *homedir) {
308309
FILE *fpin = fopen(filename, "r");
309310
if (!fpin) {
310311
fprintf(stderr, "Warning: cannot open /usr/share/applications/%s\n", filename);
312+
free(outname);
311313
if (change_exec)
312314
free(change_exec);
313315
continue;
@@ -317,6 +319,7 @@ void fix_desktop_files(const char *homedir) {
317319
if (!fpout) {
318320
fprintf(stderr, "Warning: cannot open ~/.local/share/applications/%s\n", outname);
319321
fclose(fpin);
322+
free(outname);
320323
if (change_exec)
321324
free(change_exec);
322325
continue;

src/firejail/bandwidth.c

+3
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ static void read_bandwidth_file(pid_t pid) {
198198

199199
fclose(fp);
200200
}
201+
202+
free(fname);
201203
}
202204

203205
static void write_bandwidth_file(pid_t pid) {
@@ -217,6 +219,7 @@ static void write_bandwidth_file(pid_t pid) {
217219
ptr = ptr->next;
218220
}
219221
fclose(fp);
222+
free(fname);
220223
}
221224
else
222225
goto errout;

src/firejail/fs_home.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ static void skel(const char *homedir) {
6767
if (asprintf(&fname, "%s/.zshrc", homedir) == -1)
6868
errExit("asprintf");
6969
// don't copy it if we already have the file
70-
if (access(fname, F_OK) == 0)
70+
if (access(fname, F_OK) == 0) {
71+
free(fname);
7172
return;
73+
}
7274
if (is_link(fname)) { // access(3) on dangling symlinks fails, try again using lstat
7375
fprintf(stderr, "Error: invalid %s file\n", fname);
7476
exit(1);
@@ -91,8 +93,10 @@ static void skel(const char *homedir) {
9193
if (asprintf(&fname, "%s/.cshrc", homedir) == -1)
9294
errExit("asprintf");
9395
// don't copy it if we already have the file
94-
if (access(fname, F_OK) == 0)
96+
if (access(fname, F_OK) == 0) {
97+
free(fname);
9598
return;
99+
}
96100
if (is_link(fname)) { // access(3) on dangling symlinks fails, try again using lstat
97101
fprintf(stderr, "Error: invalid %s file\n", fname);
98102
exit(1);
@@ -115,8 +119,10 @@ static void skel(const char *homedir) {
115119
if (asprintf(&fname, "%s/.bashrc", homedir) == -1)
116120
errExit("asprintf");
117121
// don't copy it if we already have the file
118-
if (access(fname, F_OK) == 0)
122+
if (access(fname, F_OK) == 0) {
123+
free(fname);
119124
return;
125+
}
120126
if (is_link(fname)) { // access(3) on dangling symlinks fails, try again using lstat
121127
fprintf(stderr, "Error: invalid %s file\n", fname);
122128
exit(1);

src/firejail/ids.c

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ static void ids_init(void) {
4242
if (dup(fd) != STDOUT_FILENO)
4343
errExit("dup");
4444
close(fd);
45+
free(fname);
4546

4647
sbox_run(SBOX_USER | SBOX_CAPS_NONE | SBOX_SECCOMP, 3, PATH_FIDS, "--init", cfg.homedir);
4748
}
@@ -63,6 +64,7 @@ static void ids_check(void) {
6364
if (dup(fd) != STDIN_FILENO)
6465
errExit("dup");
6566
close(fd);
67+
free(fname);
6668

6769
sbox_run(SBOX_USER | SBOX_CAPS_NONE | SBOX_SECCOMP| SBOX_ALLOW_STDIN, 3, PATH_FIDS, "--check", cfg.homedir);
6870
}

src/firejail/run_files.c

+2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ void set_name_run_file(pid_t pid) {
122122
// mode and ownership
123123
SET_PERMS_STREAM(fp, 0, 0, 0644);
124124
fclose(fp);
125+
free(fname);
125126
}
126127

127128

@@ -141,6 +142,7 @@ void set_x11_run_file(pid_t pid, int display) {
141142
// mode and ownership
142143
SET_PERMS_STREAM(fp, 0, 0, 0644);
143144
fclose(fp);
145+
free(fname);
144146
}
145147

146148
void set_profile_run_file(pid_t pid, const char *fname) {

src/firejail/util.c

+1
Original file line numberDiff line numberDiff line change
@@ -1392,6 +1392,7 @@ void enter_network_namespace(pid_t pid) {
13921392
fprintf(stderr, "Error: the sandbox doesn't use a new network namespace\n");
13931393
exit(1);
13941394
}
1395+
free(name);
13951396

13961397
// join the namespace
13971398
EUID_ROOT();

src/firemon/netstats.c

+2
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,12 @@ static void print_proc(int index, int itv, int col) {
152152
struct stat s;
153153
if (stat(name, &s) == -1) {
154154
// the sandbox doesn't have a --net= option, don't print
155+
free(name);
155156
if (cmd)
156157
free(cmd);
157158
return;
158159
}
160+
free(name);
159161

160162
// pid
161163
char pidstr[11];

src/jailcheck/access.c

+3
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,13 @@ void access_setup(const char *directory) {
8080
FILE *fp = fopen(test_file, "w");
8181
if (!fp) {
8282
printf("Warning: I cannot create test file in directory %s, skipping...\n", directory);
83+
free(test_file);
84+
free(path);
8385
return;
8486
}
8587
fprintf(fp, "this file was created by firetest utility, you can safely delete it\n");
8688
fclose(fp);
89+
free(path);
8790
int rv = chown(test_file, user_uid, user_gid);
8891
if (rv)
8992
errExit("chown");

src/jailcheck/noexec.c

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ void noexec_setup(void) {
5555
execfile_len = s.st_size;
5656
close(fd);
5757
}
58+
free(self);
5859
}
5960
}
6061

@@ -110,4 +111,5 @@ void noexec_test(const char *path) {
110111
wait(&status);
111112
int rv = unlink(fname);
112113
(void) rv;
114+
free(fname);
113115
}

src/jailcheck/virtual.c

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ void virtual_setup(const char *directory) {
4949
FILE *fp = fopen(test_file, "w");
5050
if (!fp) {
5151
printf("Warning: I cannot create test file in directory %s, skipping...\n", directory);
52+
free(test_file);
5253
return;
5354
}
5455
fprintf(fp, "this file was created by firetest utility, you can safely delete it\n");

0 commit comments

Comments
 (0)