Skip to content

Commit 46aaf00

Browse files
author
Ilya Pankratov
committed
Fix resource leaks and a typo
Signed-off-by: Ilya Pankratov <[email protected]>
1 parent 53ad8d7 commit 46aaf00

File tree

13 files changed

+34
-6
lines changed

13 files changed

+34
-6
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
@@ -302,6 +302,7 @@ void fix_desktop_files(const char *homedir) {
302302
printf(" %s skipped: file exists\n", filename);
303303
if (change_exec)
304304
free(change_exec);
305+
free(outname);
305306
continue;
306307
}
307308

@@ -310,6 +311,7 @@ void fix_desktop_files(const char *homedir) {
310311
fprintf(stderr, "Warning: cannot open /usr/share/applications/%s\n", filename);
311312
if (change_exec)
312313
free(change_exec);
314+
free(outname);
313315
continue;
314316
}
315317

@@ -319,6 +321,7 @@ void fix_desktop_files(const char *homedir) {
319321
fclose(fpin);
320322
if (change_exec)
321323
free(change_exec);
324+
free(outname);
322325
continue;
323326
}
324327
fprintf(fpout, "# converted by firecfg\n");

src/firejail/bandwidth.c

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

199199
fclose(fp);
200200
}
201+
free(fname);
201202
}
202203

203204
static void write_bandwidth_file(pid_t pid) {
@@ -217,6 +218,7 @@ static void write_bandwidth_file(pid_t pid) {
217218
ptr = ptr->next;
218219
}
219220
fclose(fp);
221+
free(fname);
220222
}
221223
else
222224
goto errout;

src/firejail/fs.c

+5
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ void fs_blacklist(void) {
293293
while (entry) {
294294
OPERATION op = OPERATION_MAX;
295295
char *ptr;
296+
int free_macros = 0;
296297

297298
// whitelist commands handled by fs_whitelist()
298299
if (strncmp(entry->data, "whitelist ", 10) == 0 ||
@@ -359,6 +360,7 @@ void fs_blacklist(void) {
359360
if (!enames)
360361
errExit("calloc");
361362
enames[0] = expand_macros(entry->data + 12);
363+
free_macros = 1;
362364
assert(enames[1] == 0);
363365
}
364366

@@ -372,6 +374,9 @@ void fs_blacklist(void) {
372374
noblacklist[noblacklist_c++] = enames[i];
373375
}
374376

377+
if (free_macros) {
378+
free(enames[0]);
379+
}
375380
free(enames);
376381

377382
entry = entry->next;

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-1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ static void print_proc(int index, int itv, int col) {
154154
// the sandbox doesn't have a --net= option, don't print
155155
if (cmd)
156156
free(cmd);
157+
free(name);
157158
return;
158159
}
159160

@@ -189,7 +190,7 @@ static void print_proc(int index, int itv, int col) {
189190
free(cmd);
190191
if (user)
191192
free(user);
192-
193+
free(name);
193194
}
194195

195196
void netstats(void) {

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(path);
84+
free(test_file);
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

@@ -109,5 +110,6 @@ void noexec_test(const char *path) {
109110
int status;
110111
wait(&status);
111112
int rv = unlink(fname);
113+
free(fname);
112114
(void) rv;
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");

src/profstats/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ int main(int argc, char **argv) {
344344
if (cnt_seccomp > (seccomp + 1))
345345
cnt_seccomp = seccomp + 1;
346346
if (cnt_restrict_namespaces > (restrict_namespaces + 1))
347-
cnt_seccomp = restrict_namespaces + 1;
347+
cnt_restrict_namespaces = restrict_namespaces + 1;
348348
if (cnt_dbus_user_none > (dbususernone + 1))
349349
cnt_dbus_user_none = dbususernone + 1;
350350
if (cnt_dbus_user_filter > (dbususerfilter + 1))

0 commit comments

Comments
 (0)