Skip to content

Commit 3f6ec85

Browse files
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() Signed-off-by: Ahelenia Ziemiańska <[email protected]>
1 parent c7c1bcc commit 3f6ec85

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
@@ -811,7 +811,7 @@ libzfs_mnttab_update(libzfs_handle_t *hdl)
811811
struct mnttab entry;
812812

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

817817
while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
@@ -882,7 +882,7 @@ libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
882882
libzfs_mnttab_fini(hdl);
883883

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

888888
srch.mnt_special = (char *)fsname;

lib/libzfs/libzfs_mount.c

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

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

15231523
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)