git: a9e7a44c2436 - main - makefs: Add some validation of ZFS pool names

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Fri, 16 Dec 2022 16:02:14 UTC
The branch main has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=a9e7a44c243671647180160fc448a3ef3950f55c

commit a9e7a44c243671647180160fc448a3ef3950f55c
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2022-12-16 15:25:35 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2022-12-16 15:30:58 +0000

    makefs: Add some validation of ZFS pool names
    
    Reported by:    imp
---
 usr.sbin/makefs/zfs.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/usr.sbin/makefs/zfs.c b/usr.sbin/makefs/zfs.c
index b92d08734d59..e86f838e8b5c 100644
--- a/usr.sbin/makefs/zfs.c
+++ b/usr.sbin/makefs/zfs.c
@@ -33,6 +33,7 @@
 #include <sys/queue.h>
 
 #include <assert.h>
+#include <ctype.h>
 #include <fcntl.h>
 #include <stdalign.h>
 #include <stdbool.h>
@@ -215,6 +216,19 @@ zfs_check_opts(fsinfo_t *fsopts)
 
 	if (zfs->poolname == NULL)
 		errx(1, "a pool name must be specified");
+	if (!isalpha(zfs->poolname[0]))
+		errx(1, "the pool name must begin with a letter");
+	for (size_t i = 0, len = strlen(zfs->poolname); i < len; i++) {
+		if (!isalnum(zfs->poolname[i]) && zfs->poolname[i] != '_')
+			errx(1, "invalid character '%c' in pool name",
+			    zfs->poolname[i]);
+	}
+	if (strcmp(zfs->poolname, "mirror") == 0 ||
+	    strcmp(zfs->poolname, "raidz") == 0 ||
+	    strcmp(zfs->poolname, "draid") == 0) {
+		errx(1, "pool name '%s' is reserved and cannot be used",
+		    zfs->poolname);
+	}
 
 	if (zfs->rootpath == NULL)
 		easprintf(&zfs->rootpath, "/%s", zfs->poolname);