Skip to content

Commit f831d13

Browse files
nabijaczlewelitonyhutter
authored andcommitted
libzfs{,_core}: set O_CLOEXEC on persistent (ZFS_DEV and MNTTAB) fds
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 <[email protected]> Signed-off-by: Ahelenia Ziemiańska <[email protected]> Closes #11866
1 parent a559c3d commit f831d13

File tree

4 files changed

+7
-7
lines changed

4 files changed

+7
-7
lines changed

lib/libzfs/libzfs_dataset.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ libzfs_mnttab_update(libzfs_handle_t *hdl)
813813
struct mnttab entry;
814814

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

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

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

890890
srch.mnt_special = (char *)fsname;

lib/libzfs/libzfs_mount.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1496,7 +1496,7 @@ zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
14961496
namelen = strlen(zhp->zpool_name);
14971497

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

15021502
used = alloc = 0;

lib/libzfs/libzfs_util.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,15 +1024,15 @@ libzfs_init(void)
10241024
return (NULL);
10251025
}
10261026

1027-
if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR|O_EXCL)) < 0) {
1027+
if ((hdl->libzfs_fd = open(ZFS_DEV, O_RDWR|O_EXCL|O_CLOEXEC)) < 0) {
10281028
free(hdl);
10291029
return (NULL);
10301030
}
10311031

10321032
#ifdef HAVE_SETMNTENT
1033-
if ((hdl->libzfs_mnttab = setmntent(MNTTAB, "r")) == NULL) {
1033+
if ((hdl->libzfs_mnttab = setmntent(MNTTAB, "re")) == NULL) {
10341034
#else
1035-
if ((hdl->libzfs_mnttab = fopen(MNTTAB, "r")) == NULL) {
1035+
if ((hdl->libzfs_mnttab = fopen(MNTTAB, "re")) == NULL) {
10361036
#endif
10371037
(void) close(hdl->libzfs_fd);
10381038
free(hdl);

lib/libzfs_core/libzfs_core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ libzfs_core_init(void)
137137
{
138138
(void) pthread_mutex_lock(&g_lock);
139139
if (g_refcount == 0) {
140-
g_fd = open(ZFS_DEV, O_RDWR);
140+
g_fd = open(ZFS_DEV, O_RDWR|O_CLOEXEC);
141141
if (g_fd < 0) {
142142
(void) pthread_mutex_unlock(&g_lock);
143143
return (errno);

0 commit comments

Comments
 (0)