aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorнаб <[email protected]>2021-05-22 17:19:14 +0200
committerBrian Behlendorf <[email protected]>2021-06-11 09:10:21 -0700
commitfeb04e66802ef96aa77951c43d4b632b376041ef (patch)
tree0d78f6283f001f8f66828bef33c9ace4185f7802 /lib
parent64dfdaba372f07f91a6eab598b3480693b1d14c8 (diff)
Forbid basename(3) and dirname(3)
There are at least two interpretations of basename(3), in addition to both functions being allowed to /both/ return a static buffer (unsuitable in multi-threaded environments) /and/ raze the input (which encourages overallocations, at best) Reviewed-by: John Kennedy <[email protected]> Reviewed-by: Brian Behlendorf <[email protected]> Reviewed-by: Ryan Moeller <[email protected]> Signed-off-by: Ahelenia Ziemiańska <[email protected]> Closes #12105
Diffstat (limited to 'lib')
-rw-r--r--lib/libzfs/libzfs_pool.c2
-rw-r--r--lib/libzpool/kernel.c18
-rw-r--r--lib/libzutil/zutil_import.c37
3 files changed, 33 insertions, 24 deletions
diff --git a/lib/libzfs/libzfs_pool.c b/lib/libzfs/libzfs_pool.c
index adc36c47f..c0bf9d067 100644
--- a/lib/libzfs/libzfs_pool.c
+++ b/lib/libzfs/libzfs_pool.c
@@ -4309,7 +4309,7 @@ zfs_save_arguments(int argc, char **argv, char *string, int len)
{
int i;
- (void) strlcpy(string, basename(argv[0]), len);
+ (void) strlcpy(string, zfs_basename(argv[0]), len);
for (i = 1; i < argc; i++) {
(void) strlcat(string, " ", len);
(void) strlcat(string, argv[i], len);
diff --git a/lib/libzpool/kernel.c b/lib/libzpool/kernel.c
index cc8e534e7..836eb176e 100644
--- a/lib/libzpool/kernel.c
+++ b/lib/libzpool/kernel.c
@@ -31,6 +31,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <libzutil.h>
#include <sys/crypto/icp.h>
#include <sys/processor.h>
#include <sys/rrwlock.h>
@@ -541,19 +542,10 @@ void
__dprintf(boolean_t dprint, const char *file, const char *func,
int line, const char *fmt, ...)
{
- const char *newfile;
- va_list adx;
-
- /*
- * Get rid of annoying "../common/" prefix to filename.
- */
- newfile = strrchr(file, '/');
- if (newfile != NULL) {
- newfile = newfile + 1; /* Get rid of leading / */
- } else {
- newfile = file;
- }
+ /* Get rid of annoying "../common/" prefix to filename. */
+ const char *newfile = zfs_basename(file);
+ va_list adx;
if (dprint) {
/* dprintf messages are printed immediately */
@@ -1040,7 +1032,7 @@ zfs_file_open(const char *path, int flags, int mode, zfs_file_t **fpp)
if (vn_dumpdir != NULL) {
char *dumppath = umem_zalloc(MAXPATHLEN, UMEM_NOFAIL);
- char *inpath = basename((char *)(uintptr_t)path);
+ const char *inpath = zfs_basename(path);
(void) snprintf(dumppath, MAXPATHLEN,
"%s/%s", vn_dumpdir, inpath);
diff --git a/lib/libzutil/zutil_import.c b/lib/libzutil/zutil_import.c
index 871a75ab2..9bd12973f 100644
--- a/lib/libzutil/zutil_import.c
+++ b/lib/libzutil/zutil_import.c
@@ -154,6 +154,17 @@ zutil_strdup(libpc_handle_t *hdl, const char *str)
return (ret);
}
+static char *
+zutil_strndup(libpc_handle_t *hdl, const char *str, size_t n)
+{
+ char *ret;
+
+ if ((ret = strndup(str, n)) == NULL)
+ (void) zutil_no_memory(hdl);
+
+ return (ret);
+}
+
/*
* Intermediate structures used to gather configuration information.
*/
@@ -1272,20 +1283,22 @@ zpool_find_import_scan_path(libpc_handle_t *hdl, pthread_mutex_t *lock,
{
int error = 0;
char path[MAXPATHLEN];
- char *d, *b;
- char *dpath, *name;
+ char *d = NULL;
+ ssize_t dl;
+ const char *dpath, *name;
/*
- * Separate the directory part and last part of the
- * path. We do this so that we can get the realpath of
+ * Separate the directory and the basename.
+ * We do this so that we can get the realpath of
* the directory. We don't get the realpath on the
* whole path because if it's a symlink, we want the
* path of the symlink not where it points to.
*/
- d = zutil_strdup(hdl, dir);
- b = zutil_strdup(hdl, dir);
- dpath = dirname(d);
- name = basename(b);
+ name = zfs_basename(dir);
+ if ((dl = zfs_dirnamelen(dir)) == -1)
+ dpath = ".";
+ else
+ dpath = d = zutil_strndup(hdl, dir, dl);
if (realpath(dpath, path) == NULL) {
error = errno;
@@ -1303,7 +1316,6 @@ zpool_find_import_scan_path(libpc_handle_t *hdl, pthread_mutex_t *lock,
zpool_find_import_scan_add_slice(hdl, lock, cache, path, name, order);
out:
- free(b);
free(d);
return (error);
}
@@ -1506,6 +1518,7 @@ discover_cached_paths(libpc_handle_t *hdl, nvlist_t *nv,
avl_tree_t *cache, pthread_mutex_t *lock)
{
char *path = NULL;
+ ssize_t dl;
uint_t children;
nvlist_t **child;
@@ -1521,8 +1534,12 @@ discover_cached_paths(libpc_handle_t *hdl, nvlist_t *nv,
* our directory cache.
*/
if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
+ if ((dl = zfs_dirnamelen(path)) == -1)
+ path = ".";
+ else
+ path[dl] = '\0';
return (zpool_find_import_scan_dir(hdl, lock, cache,
- dirname(path), 0));
+ path, 0));
}
return (0);
}