Skip to content

Commit

Permalink
libzfs{,_core}: set O_CLOEXEC on persistent (ZFS_DEV and MNTTAB) fds
Browse files Browse the repository at this point in the history
These were fd 3, 4, and 5 by the time zfs change-key hit
execute_key_fob()

glibc appends "e" to setmntent() mode, but musl's just returns fopen()

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
Closes openzfs#11866
  • Loading branch information
nabijaczleweli authored and Ryan Moeller committed May 6, 2021
1 parent d8ca13a commit 29ac1e3
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/libzfs/libzfs_dataset.c
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ libzfs_mnttab_update(libzfs_handle_t *hdl)
struct mnttab entry;

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

while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
Expand Down Expand Up @@ -884,7 +884,7 @@ libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
libzfs_mnttab_fini(hdl);

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

srch.mnt_special = (char *)fsname;
Expand Down
2 changes: 1 addition & 1 deletion lib/libzfs/libzfs_mount.c
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,7 @@ zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
namelen = strlen(zhp->zpool_name);

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

used = alloc = 0;
Expand Down
6 changes: 3 additions & 3 deletions lib/libzfs/libzfs_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -1024,15 +1024,15 @@ libzfs_init(void)
return (NULL);
}

if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR|O_EXCL)) < 0) {
if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR|O_EXCL|O_CLOEXEC)) < 0) {
free(hdl);
return (NULL);
}

#ifdef HAVE_SETMNTENT
if ((hdl->libzfs_mnttab = setmntent(MNTTAB, "r")) == NULL) {
if ((hdl->libzfs_mnttab = setmntent(MNTTAB, "re")) == NULL) {
#else
if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
if ((hdl->libzfs_mnttab = fopen(MNTTAB, "re")) == NULL) {
#endif
(void) close(hdl->libzfs_fd);
free(hdl);
Expand Down
2 changes: 1 addition & 1 deletion lib/libzfs_core/libzfs_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ libzfs_core_init(void)
{
(void) pthread_mutex_lock(&g_lock);
if (g_refcount == 0) {
g_fd = open(ZFS_DEV, O_RDWR);
g_fd = open(ZFS_DEV, O_RDWR|O_CLOEXEC);
if (g_fd < 0) {
(void) pthread_mutex_unlock(&g_lock);
return (errno);
Expand Down

0 comments on commit 29ac1e3

Please sign in to comment.