Skip to content

Commit e57100b

Browse files
committed
Consistently use cleanup_fdp() to close fds when the result is ignored
This avoids leaving dangling references to fds that no longer exist, clarifying ownership. This commit does not cover the socket pairs used to transfer the pid of a descendant process (see containers#665 for that) and privilege-separated operations (see containers#666). Signed-off-by: Simon McVittie <[email protected]>
1 parent 4021ac2 commit e57100b

File tree

2 files changed

+22
-45
lines changed

2 files changed

+22
-45
lines changed

bubblewrap.c

+16-39
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,7 @@ seccomp_program_new (int *fd)
268268
if (data == NULL)
269269
die_with_error ("Can't read seccomp data");
270270

271-
close (*fd);
272-
*fd = -1;
271+
cleanup_fdp (fd);
273272

274273
if (len % 8 != 0)
275274
die ("Invalid seccomp data, must be multiple of 8");
@@ -481,8 +480,7 @@ report_child_exit_status (int exitc, int setup_finished_fd)
481480

482481
output = xasprintf ("{ \"exit-code\": %i }\n", exitc);
483482
dump_info (opt_json_status_fd, output, false);
484-
close (opt_json_status_fd);
485-
opt_json_status_fd = -1;
483+
cleanup_fdp (&opt_json_status_fd);
486484
close (setup_finished_fd);
487485
}
488486

@@ -657,13 +655,7 @@ do_init (int event_fd, pid_t initial_pid)
657655

658656
/* Close FDs. */
659657
for (lock = lock_files; lock != NULL; lock = lock->next)
660-
{
661-
if (lock->fd >= 0)
662-
{
663-
close (lock->fd);
664-
lock->fd = -1;
665-
}
666-
}
658+
cleanup_fdp (&lock->fd);
667659

668660
return initial_exit_status;
669661
}
@@ -1505,8 +1497,7 @@ setup_newroot (bool unshare_pid,
15051497
if (copy_file_data (op->fd, dest_fd) != 0)
15061498
die_with_error ("Can't write data to file %s", op->dest);
15071499

1508-
close (op->fd);
1509-
op->fd = -1;
1500+
cleanup_fdp (&op->fd);
15101501
}
15111502
break;
15121503

@@ -1531,8 +1522,7 @@ setup_newroot (bool unshare_pid,
15311522
if (copy_file_data (op->fd, dest_fd) != 0)
15321523
die_with_error ("Can't write data to file %s", op->dest);
15331524

1534-
close (op->fd);
1535-
op->fd = -1;
1525+
cleanup_fdp (&op->fd);
15361526

15371527
assert (dest != NULL);
15381528

@@ -1598,13 +1588,7 @@ close_ops_fd (void)
15981588
SetupOp *op;
15991589

16001590
for (op = ops; op != NULL; op = op->next)
1601-
{
1602-
if (op->fd != -1)
1603-
{
1604-
(void) close (op->fd);
1605-
op->fd = -1;
1606-
}
1607-
}
1591+
cleanup_fdp (&op->fd);
16081592
}
16091593

16101594
/* We need to resolve relative symlinks in the sandbox before we
@@ -1820,7 +1804,7 @@ parse_args_recurse (int *argcp,
18201804
opt_args_data = load_file_data (the_fd, &data_len);
18211805
if (opt_args_data == NULL)
18221806
die_with_error ("Can't read --args data");
1823-
(void) close (the_fd);
1807+
cleanup_fdp (&the_fd);
18241808

18251809
data_end = opt_args_data + data_len;
18261810
data_argc = 0;
@@ -3186,7 +3170,7 @@ main (int argc,
31863170
dump_info (opt_info_fd, output, true);
31873171
namespace_ids_write (opt_info_fd, false);
31883172
dump_info (opt_info_fd, "\n}\n", true);
3189-
close (opt_info_fd);
3173+
cleanup_fdp (&opt_info_fd);
31903174
}
31913175
if (opt_json_status_fd != -1)
31923176
{
@@ -3200,14 +3184,14 @@ main (int argc,
32003184
{
32013185
char b[1];
32023186
(void) TEMP_FAILURE_RETRY (read (opt_userns_block_fd, b, 1));
3203-
close (opt_userns_block_fd);
3187+
cleanup_fdp (&opt_userns_block_fd);
32043188
}
32053189

32063190
/* Let child run now that the uid maps are set up */
32073191
val = 1;
32083192
res = TEMP_FAILURE_RETRY (write (child_wait_fd, &val, 8));
32093193
/* Ignore res, if e.g. the child died and closed child_wait_fd we don't want to error out here */
3210-
close (child_wait_fd);
3194+
cleanup_fdp (&child_wait_fd);
32113195

32123196
return monitor_child (event_fd, pid, setup_finished_pipe[0]);
32133197
}
@@ -3251,15 +3235,12 @@ main (int argc,
32513235
* sandboxed process from outside the sandbox either.
32523236
*/
32533237

3254-
if (opt_info_fd != -1)
3255-
close (opt_info_fd);
3256-
3257-
if (opt_json_status_fd != -1)
3258-
close (opt_json_status_fd);
3238+
cleanup_fdp (&opt_info_fd);
3239+
cleanup_fdp (&opt_json_status_fd);
32593240

32603241
/* Wait for the parent to init uid/gid maps and drop caps */
32613242
res = read (child_wait_fd, &val, 8);
3262-
close (child_wait_fd);
3243+
cleanup_fdp (&child_wait_fd);
32633244

32643245
/* At this point we can completely drop root uid, but retain the
32653246
* required permitted caps. This allow us to do full setup as
@@ -3498,7 +3479,7 @@ main (int argc,
34983479
{
34993480
char b[1];
35003481
(void) TEMP_FAILURE_RETRY (read (opt_block_fd, b, 1));
3501-
close (opt_block_fd);
3482+
cleanup_fdp (&opt_block_fd);
35023483
}
35033484

35043485
if (opt_seccomp_fd != -1)
@@ -3580,16 +3561,12 @@ main (int argc,
35803561

35813562
debug ("launch executable %s", argv[0]);
35823563

3583-
if (proc_fd != -1)
3584-
close (proc_fd);
3564+
cleanup_fdp (&proc_fd);
35853565

35863566
/* If we are using --as-pid-1 leak the sync fd into the sandbox.
35873567
--sync-fd will still work unless the container process doesn't close this file. */
35883568
if (!opt_as_pid_1)
3589-
{
3590-
if (opt_sync_fd != -1)
3591-
close (opt_sync_fd);
3592-
}
3569+
cleanup_fdp (&opt_sync_fd);
35933570

35943571
/* We want sigchild in the child */
35953572
unblock_sigchild ();

utils.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ write_file_at (int dfd,
469469
res = write_to_fd (fd, content, strlen (content));
470470

471471
errsv = errno;
472-
close (fd);
472+
cleanup_fdp (&fd);
473473
errno = errsv;
474474

475475
return res;
@@ -494,7 +494,7 @@ create_file (const char *path,
494494
res = write_to_fd (fd, content, strlen (content));
495495

496496
errsv = errno;
497-
close (fd);
497+
cleanup_fdp (&fd);
498498
errno = errsv;
499499

500500
return res;
@@ -574,16 +574,16 @@ copy_file (const char *src_path,
574574
if (dfd == -1)
575575
{
576576
errsv = errno;
577-
close (sfd);
577+
cleanup_fdp (&sfd);
578578
errno = errsv;
579579
return -1;
580580
}
581581

582582
res = copy_file_data (sfd, dfd);
583583

584584
errsv = errno;
585-
close (sfd);
586-
close (dfd);
585+
cleanup_fdp (&sfd);
586+
cleanup_fdp (&dfd);
587587
errno = errsv;
588588

589589
return res;
@@ -654,7 +654,7 @@ load_file_at (int dfd,
654654
data = load_file_data (fd, NULL);
655655

656656
errsv = errno;
657-
close (fd);
657+
cleanup_fdp (&fd);
658658
errno = errsv;
659659

660660
return data;

0 commit comments

Comments
 (0)