Skip to content

Commit 0b9399f

Browse files
lib/: set O_CLOEXEC on all fds
As found by git grep -E '(open|setmntent|pipe2?)\(' | grep -vE '((zfs|zpool)_|fd|dl|lzc_re|pidfile_|g_)open\(' FreeBSD's pidfile_open() says nothing about the flags of the files it opens, but we can't do anything about it anyway; the implementation does open all files with O_CLOEXEC Consider this output with zpool.d/media appended with "pid=$$; (ls -l /proc/$pid/fd > /dev/tty)": $ /sbin/zpool iostat -vc media lrwx------ 0 -> /dev/pts/0 l-wx------ 1 -> 'pipe:[3278500]' l-wx------ 2 -> /dev/null lrwx------ 3 -> /dev/zfs lr-x------ 4 -> /proc/31895/mounts lrwx------ 5 -> /dev/zfs lr-x------ 10 -> /usr/lib/zfs-linux/zpool.d/media vs $ ./zpool iostat -vc vendor,upath,iostat,media lrwx------ 0 -> /dev/pts/0 l-wx------ 1 -> 'pipe:[3279887]' l-wx------ 2 -> /dev/null lr-x------ 10 -> /usr/lib/zfs-linux/zpool.d/media Signed-off-by: Ahelenia Ziemiańska <[email protected]>
1 parent 3f6ec85 commit 0b9399f

File tree

22 files changed

+51
-62
lines changed

22 files changed

+51
-62
lines changed

include/sys/zfs_context.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,8 @@ extern void delay(clock_t ticks);
638638
#define NN_NUMBUF_SZ (6)
639639

640640
extern uint64_t physmem;
641-
extern char *random_path;
642-
extern char *urandom_path;
641+
extern const char *random_path;
642+
extern const char *urandom_path;
643643

644644
extern int highbit64(uint64_t i);
645645
extern int lowbit64(uint64_t i);

lib/libshare/os/freebsd/nfs.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static int
6666
nfs_exports_lock(void)
6767
{
6868
nfs_lock_fd = open(ZFS_EXPORTS_LOCK,
69-
O_RDWR | O_CREAT, 0600);
69+
O_RDWR | O_CREAT | O_CLOEXEC, 0600);
7070
if (nfs_lock_fd == -1) {
7171
fprintf(stderr, "failed to lock %s: %s\n",
7272
ZFS_EXPORTS_LOCK, strerror(errno));
@@ -228,8 +228,8 @@ nfs_copy_entries(char *filename, const char *mountpoint)
228228
int error = SA_OK;
229229
char *line;
230230

231-
FILE *oldfp = fopen(ZFS_EXPORTS_FILE, "r");
232-
FILE *newfp = fopen(filename, "w+");
231+
FILE *oldfp = fopen(ZFS_EXPORTS_FILE, "re");
232+
FILE *newfp = fopen(filename, "w+e");
233233
if (newfp == NULL) {
234234
fprintf(stderr, "failed to open %s file: %s", filename,
235235
strerror(errno));
@@ -291,7 +291,7 @@ nfs_enable_share(sa_share_impl_t impl_share)
291291
return (error);
292292
}
293293

294-
FILE *fp = fopen(filename, "a+");
294+
FILE *fp = fopen(filename, "a+e");
295295
if (fp == NULL) {
296296
fprintf(stderr, "failed to open %s file: %s", filename,
297297
strerror(errno));
@@ -368,7 +368,7 @@ nfs_is_shared(sa_share_impl_t impl_share)
368368
char *mntpoint = impl_share->sa_mountpoint;
369369
size_t mntlen = strlen(mntpoint);
370370

371-
FILE *fp = fopen(ZFS_EXPORTS_FILE, "r");
371+
FILE *fp = fopen(ZFS_EXPORTS_FILE, "re");
372372
if (fp == NULL)
373373
return (B_FALSE);
374374

lib/libshare/os/linux/nfs.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static int
6666
nfs_exports_lock(void)
6767
{
6868
nfs_lock_fd = open(ZFS_EXPORTS_LOCK,
69-
O_RDWR | O_CREAT, 0600);
69+
O_RDWR | O_CREAT | O_CLOEXEC, 0600);
7070
if (nfs_lock_fd == -1) {
7171
fprintf(stderr, "failed to lock %s: %s\n",
7272
ZFS_EXPORTS_LOCK, strerror(errno));
@@ -453,7 +453,7 @@ nfs_add_entry(const char *filename, const char *sharepath,
453453
if (linux_opts == NULL)
454454
linux_opts = "";
455455

456-
FILE *fp = fopen(filename, "a+");
456+
FILE *fp = fopen(filename, "a+e");
457457
if (fp == NULL) {
458458
fprintf(stderr, "failed to open %s file: %s", filename,
459459
strerror(errno));
@@ -489,8 +489,8 @@ nfs_copy_entries(char *filename, const char *mountpoint)
489489
size_t buflen = 0;
490490
int error = SA_OK;
491491

492-
FILE *oldfp = fopen(ZFS_EXPORTS_FILE, "r");
493-
FILE *newfp = fopen(filename, "w+");
492+
FILE *oldfp = fopen(ZFS_EXPORTS_FILE, "re");
493+
FILE *newfp = fopen(filename, "w+e");
494494
if (newfp == NULL) {
495495
fprintf(stderr, "failed to open %s file: %s", filename,
496496
strerror(errno));
@@ -632,7 +632,7 @@ nfs_is_shared(sa_share_impl_t impl_share)
632632
size_t buflen = 0;
633633
char *buf = NULL;
634634

635-
FILE *fp = fopen(ZFS_EXPORTS_FILE, "r");
635+
FILE *fp = fopen(ZFS_EXPORTS_FILE, "re");
636636
if (fp == NULL) {
637637
return (B_FALSE);
638638
}

lib/libshare/os/linux/smb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ smb_retrieve_shares(void)
107107
if (!S_ISREG(eStat.st_mode))
108108
continue;
109109

110-
if ((share_file_fp = fopen(file_path, "r")) == NULL) {
110+
if ((share_file_fp = fopen(file_path, "re")) == NULL) {
111111
rc = SA_SYSTEM_ERR;
112112
goto out;
113113
}

lib/libspl/os/linux/gethostid.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ get_spl_hostid(void)
4545
return (hostid & HOSTID_MASK);
4646
}
4747

48-
f = fopen("/sys/module/spl/parameters/spl_hostid", "r");
48+
f = fopen("/sys/module/spl/parameters/spl_hostid", "re");
4949
if (!f)
5050
return (0);
5151

@@ -74,7 +74,7 @@ get_system_hostid(void)
7474
unsigned long hostid;
7575
int hostid_size = 4; /* 4 bytes regardless of arch */
7676

77-
fd = open("/etc/hostid", O_RDONLY);
77+
fd = open("/etc/hostid", O_RDONLY | O_CLOEXEC);
7878
if (fd >= 0) {
7979
rc = read(fd, &hostid, hostid_size);
8080
if (rc > 0)

lib/libspl/os/linux/getmntany.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ getextmntent(const char *path, struct extmnttab *entry, struct stat64 *statbuf)
128128

129129

130130
#ifdef HAVE_SETMNTENT
131-
if ((fp = setmntent(MNTTAB, "r")) == NULL) {
131+
if ((fp = setmntent(MNTTAB, "re")) == NULL) {
132132
#else
133-
if ((fp = fopen(MNTTAB, "r")) == NULL) {
133+
if ((fp = fopen(MNTTAB, "re")) == NULL) {
134134
#endif
135135
(void) fprintf(stderr, "cannot open %s\n", MNTTAB);
136136
return (-1);

lib/libuutil/uu_open.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,6 @@
3636
#include <stdio.h>
3737
#include <unistd.h>
3838

39-
#ifdef _LP64
40-
#define TMPPATHFMT "%s/uu%ld"
41-
#else /* _LP64 */
42-
#define TMPPATHFMT "%s/uu%lld"
43-
#endif /* _LP64 */
44-
4539
/*ARGSUSED*/
4640
int
4741
uu_open_tmp(const char *dir, uint_t uflags)
@@ -55,7 +49,7 @@ uu_open_tmp(const char *dir, uint_t uflags)
5549
for (;;) {
5650
(void) snprintf(fname, PATH_MAX, "%s/uu%lld", dir, gethrtime());
5751

58-
f = open(fname, O_CREAT | O_EXCL | O_RDWR, 0600);
52+
f = open(fname, O_CREAT | O_EXCL | O_RDWR | O_CLOEXEC, 0600);
5953

6054
if (f >= 0 || errno != EEXIST)
6155
break;

lib/libzfs/libzfs_crypto.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pkcs11_get_urandom(uint8_t *buf, size_t bytes)
7171
int rand;
7272
ssize_t bytes_read = 0;
7373

74-
rand = open("/dev/urandom", O_RDONLY);
74+
rand = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
7575

7676
if (rand < 0)
7777
return (rand);
@@ -468,7 +468,7 @@ get_key_material_file(libzfs_handle_t *hdl, const char *uri,
468468
if (strlen(uri) < 7)
469469
return (EINVAL);
470470

471-
if ((f = fopen(uri + 7, "r")) == NULL) {
471+
if ((f = fopen(uri + 7, "re")) == NULL) {
472472
ret = errno;
473473
errno = 0;
474474
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,

lib/libzfs/libzfs_diff.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ setup_differ_info(zfs_handle_t *zhp, const char *fromsnap,
697697
{
698698
di->zhp = zhp;
699699

700-
di->cleanupfd = open(ZFS_DEV, O_RDWR);
700+
di->cleanupfd = open(ZFS_DEV, O_RDWR | O_CLOEXEC);
701701
VERIFY(di->cleanupfd >= 0);
702702

703703
if (get_snapshot_names(di, fromsnap, tosnap) != 0)
@@ -731,7 +731,7 @@ zfs_show_diffs(zfs_handle_t *zhp, int outfd, const char *fromsnap,
731731
return (-1);
732732
}
733733

734-
if (pipe(pipefd)) {
734+
if (pipe2(pipefd, O_CLOEXEC)) {
735735
zfs_error_aux(zhp->zfs_hdl, strerror(errno));
736736
teardown_differ_info(&di);
737737
return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED, errbuf));

lib/libzfs/libzfs_iter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ zfs_iter_mounted(zfs_handle_t *zhp, zfs_iter_f func, void *data)
565565
FILE *mnttab;
566566
int err = 0;
567567

568-
if ((mnttab = fopen(MNTTAB, "r")) == NULL)
568+
if ((mnttab = fopen(MNTTAB, "re")) == NULL)
569569
return (ENOENT);
570570

571571
while (err == 0 && getmntent(mnttab, &entry) == 0) {

lib/libzfs/libzfs_pool.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4809,13 +4809,11 @@ zpool_load_compat(const char *compatibility,
48094809
* as they're only needed if the filename is relative
48104810
* which will be checked during the openat().
48114811
*/
4812-
#ifdef O_PATH
4813-
sdirfd = open(ZPOOL_SYSCONF_COMPAT_D, O_DIRECTORY | O_PATH);
4814-
ddirfd = open(ZPOOL_DATA_COMPAT_D, O_DIRECTORY | O_PATH);
4815-
#else
4816-
sdirfd = open(ZPOOL_SYSCONF_COMPAT_D, O_DIRECTORY | O_RDONLY);
4817-
ddirfd = open(ZPOOL_DATA_COMPAT_D, O_DIRECTORY | O_RDONLY);
4812+
#ifndef O_PATH
4813+
#define O_PATH O_RDONLY
48184814
#endif
4815+
sdirfd = open(ZPOOL_SYSCONF_COMPAT_D, O_DIRECTORY | O_PATH | O_CLOEXEC);
4816+
ddirfd = open(ZPOOL_DATA_COMPAT_D, O_DIRECTORY | O_PATH | O_CLOEXEC);
48194817

48204818
(void) strlcpy(filenames, compatibility, ZFS_MAXPROPLEN);
48214819
file = strtok_r(filenames, ",", &ps);

lib/libzfs/libzfs_sendrecv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2207,7 +2207,7 @@ zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
22072207
++holdseq;
22082208
(void) snprintf(sdd.holdtag, sizeof (sdd.holdtag),
22092209
".send-%d-%llu", getpid(), (u_longlong_t)holdseq);
2210-
sdd.cleanup_fd = open(ZFS_DEV, O_RDWR);
2210+
sdd.cleanup_fd = open(ZFS_DEV, O_RDWR | O_CLOEXEC);
22112211
if (sdd.cleanup_fd < 0) {
22122212
err = errno;
22132213
goto stderr_out;

lib/libzfs/libzfs_util.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -884,13 +884,13 @@ libzfs_run_process_impl(const char *path, char *argv[], char *env[], int flags,
884884
* Setup a pipe between our child and parent process if we're
885885
* reading stdout.
886886
*/
887-
if ((lines != NULL) && pipe(link) == -1)
887+
if ((lines != NULL) && pipe2(link, O_CLOEXEC) == -1)
888888
return (-EPIPE);
889889

890890
pid = vfork();
891891
if (pid == 0) {
892892
/* Child process */
893-
devnull_fd = open("/dev/null", O_WRONLY);
893+
devnull_fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
894894

895895
if (devnull_fd < 0)
896896
_exit(-1);
@@ -900,15 +900,11 @@ libzfs_run_process_impl(const char *path, char *argv[], char *env[], int flags,
900900
else if (lines != NULL) {
901901
/* Save the output to lines[] */
902902
dup2(link[1], STDOUT_FILENO);
903-
close(link[0]);
904-
close(link[1]);
905903
}
906904

907905
if (!(flags & STDERR_VERBOSE))
908906
(void) dup2(devnull_fd, STDERR_FILENO);
909907

910-
close(devnull_fd);
911-
912908
if (flags & NO_DEFAULT_PATH) {
913909
if (env == NULL)
914910
execv(path, argv);
@@ -1144,7 +1140,7 @@ zfs_path_to_zhandle(libzfs_handle_t *hdl, const char *path, zfs_type_t argtype)
11441140
}
11451141

11461142
/* Reopen MNTTAB to prevent reading stale data from open file */
1147-
if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL)
1143+
if (freopen(MNTTAB, "re", hdl->libzfs_mnttab) == NULL)
11481144
return (NULL);
11491145

11501146
if (getextmntent(path, &entry, &statbuf) != 0)

lib/libzfs/os/linux/libzfs_pool_os.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ zpool_relabel_disk(libzfs_handle_t *hdl, const char *path, const char *msg)
6262
{
6363
int fd, error;
6464

65-
if ((fd = open(path, O_RDWR|O_DIRECT)) < 0) {
65+
if ((fd = open(path, O_RDWR|O_DIRECT|O_CLOEXEC)) < 0) {
6666
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
6767
"relabel '%s': unable to open device: %d"), path, errno);
6868
return (zfs_error(hdl, EZFS_OPENFAILED, msg));
@@ -107,7 +107,7 @@ read_efi_label(nvlist_t *config, diskaddr_t *sb)
107107

108108
(void) snprintf(diskname, sizeof (diskname), "%s%s", DISK_ROOT,
109109
strrchr(path, '/'));
110-
if ((fd = open(diskname, O_RDONLY|O_DIRECT)) >= 0) {
110+
if ((fd = open(diskname, O_RDONLY|O_DIRECT|O_CLOEXEC)) >= 0) {
111111
struct dk_gpt *vtoc;
112112

113113
if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
@@ -159,7 +159,7 @@ zpool_label_disk_check(char *path)
159159
struct dk_gpt *vtoc;
160160
int fd, err;
161161

162-
if ((fd = open(path, O_RDONLY|O_DIRECT)) < 0)
162+
if ((fd = open(path, O_RDONLY|O_DIRECT|O_CLOEXEC)) < 0)
163163
return (errno);
164164

165165
if ((err = efi_alloc_and_read(fd, &vtoc)) != 0) {
@@ -190,7 +190,7 @@ zpool_label_name(char *label_name, int label_size)
190190
uint64_t id = 0;
191191
int fd;
192192

193-
fd = open("/dev/urandom", O_RDONLY);
193+
fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC);
194194
if (fd >= 0) {
195195
if (read(fd, &id, sizeof (id)) != sizeof (id))
196196
id = 0;
@@ -241,7 +241,7 @@ zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name)
241241

242242
(void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
243243

244-
if ((fd = open(path, O_RDWR|O_DIRECT|O_EXCL)) < 0) {
244+
if ((fd = open(path, O_RDWR|O_DIRECT|O_EXCL|O_CLOEXEC)) < 0) {
245245
/*
246246
* This shouldn't happen. We've long since verified that this
247247
* is a valid device.

lib/libzfs/os/linux/libzfs_sendrecv_os.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
void
3636
libzfs_set_pipe_max(int infd)
3737
{
38-
FILE *procf = fopen("/proc/sys/fs/pipe-max-size", "r");
38+
FILE *procf = fopen("/proc/sys/fs/pipe-max-size", "re");
3939

4040
if (procf != NULL) {
4141
unsigned long max_psize;

lib/libzfs/os/linux/libzfs_util_os.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ libzfs_load_module_impl(const char *module)
143143

144144
start = gethrtime();
145145
do {
146-
fd = open(ZFS_DEV, O_RDWR);
146+
fd = open(ZFS_DEV, O_RDWR | O_CLOEXEC);
147147
if (fd >= 0) {
148148
(void) close(fd);
149149
return (0);
@@ -195,7 +195,7 @@ zfs_version_kernel(char *version, int len)
195195
int fd;
196196
int rlen;
197197

198-
if ((fd = open(ZFS_SYSFS_DIR "/version", O_RDONLY)) == -1)
198+
if ((fd = open(ZFS_SYSFS_DIR "/version", O_RDONLY | O_CLOEXEC)) == -1)
199199
return (-1);
200200

201201
if ((rlen = read(fd, version, len)) == -1) {

lib/libzpool/kernel.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -723,15 +723,15 @@ lowbit64(uint64_t i)
723723
return (__builtin_ffsll(i));
724724
}
725725

726-
char *random_path = "/dev/random";
727-
char *urandom_path = "/dev/urandom";
726+
const char *random_path = "/dev/random";
727+
const char *urandom_path = "/dev/urandom";
728728
static int random_fd = -1, urandom_fd = -1;
729729

730730
void
731731
random_init(void)
732732
{
733-
VERIFY((random_fd = open(random_path, O_RDONLY)) != -1);
734-
VERIFY((urandom_fd = open(urandom_path, O_RDONLY)) != -1);
733+
VERIFY((random_fd = open(random_path, O_RDONLY | O_CLOEXEC)) != -1);
734+
VERIFY((urandom_fd = open(urandom_path, O_RDONLY | O_CLOEXEC)) != -1);
735735
}
736736

737737
void

lib/libzpool/util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ pool_active(void *unused, const char *name, uint64_t guid,
259259
* Use ZFS_IOC_POOL_SYNC to confirm if a pool is active
260260
*/
261261

262-
fd = open(ZFS_DEV, O_RDWR);
262+
fd = open(ZFS_DEV, O_RDWR | O_CLOEXEC);
263263
if (fd < 0)
264264
return (-1);
265265

lib/libzutil/os/freebsd/zutil_import_os.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ zpool_open_func(void *arg)
127127
/*
128128
* O_NONBLOCK so we don't hang trying to open things like serial ports.
129129
*/
130-
if ((fd = open(rn->rn_name, O_RDONLY|O_NONBLOCK)) < 0)
130+
if ((fd = open(rn->rn_name, O_RDONLY|O_NONBLOCK|O_CLOEXEC)) < 0)
131131
return;
132132

133133
/*

lib/libzutil/os/linux/zutil_device_path_os.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ zfs_dev_is_whole_disk(const char *dev_name)
390390
struct dk_gpt *label;
391391
int fd;
392392

393-
if ((fd = open(dev_name, O_RDONLY | O_DIRECT)) < 0)
393+
if ((fd = open(dev_name, O_RDONLY | O_DIRECT | O_CLOEXEC)) < 0)
394394
return (B_FALSE);
395395

396396
if (efi_alloc_and_init(fd, EFI_NUMPAR, &label) != 0) {

lib/libzutil/os/linux/zutil_import_os.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ zpool_open_func(void *arg)
136136
* cache which may be stale for multipath devices. An EINVAL errno
137137
* indicates O_DIRECT is unsupported so fallback to just O_RDONLY.
138138
*/
139-
fd = open(rn->rn_name, O_RDONLY | O_DIRECT);
139+
fd = open(rn->rn_name, O_RDONLY | O_DIRECT | O_CLOEXEC);
140140
if ((fd < 0) && (errno == EINVAL))
141-
fd = open(rn->rn_name, O_RDONLY);
141+
fd = open(rn->rn_name, O_RDONLY | O_CLOEXEC);
142142
if ((fd < 0) && (errno == EACCES))
143143
hdl->lpc_open_access_error = B_TRUE;
144144
if (fd < 0)

0 commit comments

Comments
 (0)