xref: /titanic_52/usr/src/common/zfs/zfs_namecheck.c (revision 40a5c998e5462ed47916e2edfcc5016073cf8851)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
51d452cf5Sahrens  * Common Development and Distribution License (the "License").
61d452cf5Sahrens  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
2214843421SMatthew Ahrens  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
2578f17100SMatthew Ahrens /*
2678f17100SMatthew Ahrens  * Copyright (c) 2013 by Delphix. All rights reserved.
2778f17100SMatthew Ahrens  */
28fa9e4066Sahrens 
29fa9e4066Sahrens /*
30fa9e4066Sahrens  * Common name validation routines for ZFS.  These routines are shared by the
31fa9e4066Sahrens  * userland code as well as the ioctl() layer to ensure that we don't
32fa9e4066Sahrens  * inadvertently expose a hole through direct ioctl()s that never gets tested.
33fa9e4066Sahrens  * In userland, however, we want significantly more information about _why_ the
34fa9e4066Sahrens  * name is invalid.  In the kernel, we only care whether it's valid or not.
35fa9e4066Sahrens  * Each routine therefore takes a 'namecheck_err_t' which describes exactly why
36fa9e4066Sahrens  * the name failed to validate.
37fa9e4066Sahrens  *
38fa9e4066Sahrens  * Each function returns 0 on success, -1 on error.
39fa9e4066Sahrens  */
40fa9e4066Sahrens 
41fa9e4066Sahrens #if defined(_KERNEL)
42fa9e4066Sahrens #include <sys/systm.h>
43fa9e4066Sahrens #else
44fa9e4066Sahrens #include <string.h>
45fa9e4066Sahrens #endif
46fa9e4066Sahrens 
47b81d61a6Slling #include <sys/param.h>
48ecd6cf80Smarks #include <sys/nvpair.h>
49fa9e4066Sahrens #include "zfs_namecheck.h"
50ecd6cf80Smarks #include "zfs_deleg.h"
51fa9e4066Sahrens 
52fa9e4066Sahrens static int
53fa9e4066Sahrens valid_char(char c)
54fa9e4066Sahrens {
55fa9e4066Sahrens 	return ((c >= 'a' && c <= 'z') ||
56fa9e4066Sahrens 	    (c >= 'A' && c <= 'Z') ||
57fa9e4066Sahrens 	    (c >= '0' && c <= '9') ||
586d1e0b89Smarks 	    c == '-' || c == '_' || c == '.' || c == ':' || c == ' ');
59fa9e4066Sahrens }
60fa9e4066Sahrens 
61fa9e4066Sahrens /*
621d452cf5Sahrens  * Snapshot names must be made up of alphanumeric characters plus the following
631d452cf5Sahrens  * characters:
641d452cf5Sahrens  *
65f0ed2251Sek110237  * 	[-_.: ]
661d452cf5Sahrens  */
671d452cf5Sahrens int
6878f17100SMatthew Ahrens zfs_component_namecheck(const char *path, namecheck_err_t *why, char *what)
691d452cf5Sahrens {
701d452cf5Sahrens 	const char *loc;
711d452cf5Sahrens 
72*40a5c998SMatthew Ahrens 	if (strlen(path) >= ZFS_MAX_DATASET_NAME_LEN) {
731d452cf5Sahrens 		if (why)
741d452cf5Sahrens 			*why = NAME_ERR_TOOLONG;
751d452cf5Sahrens 		return (-1);
761d452cf5Sahrens 	}
771d452cf5Sahrens 
781d452cf5Sahrens 	if (path[0] == '\0') {
791d452cf5Sahrens 		if (why)
801d452cf5Sahrens 			*why = NAME_ERR_EMPTY_COMPONENT;
811d452cf5Sahrens 		return (-1);
821d452cf5Sahrens 	}
831d452cf5Sahrens 
841d452cf5Sahrens 	for (loc = path; *loc; loc++) {
851d452cf5Sahrens 		if (!valid_char(*loc)) {
861d452cf5Sahrens 			if (why) {
871d452cf5Sahrens 				*why = NAME_ERR_INVALCHAR;
881d452cf5Sahrens 				*what = *loc;
891d452cf5Sahrens 			}
901d452cf5Sahrens 			return (-1);
911d452cf5Sahrens 		}
921d452cf5Sahrens 	}
931d452cf5Sahrens 	return (0);
941d452cf5Sahrens }
951d452cf5Sahrens 
96ecd6cf80Smarks 
97ecd6cf80Smarks /*
98ecd6cf80Smarks  * Permissions set name must start with the letter '@' followed by the
99ecd6cf80Smarks  * same character restrictions as snapshot names, except that the name
100ecd6cf80Smarks  * cannot exceed 64 characters.
101ecd6cf80Smarks  */
102ecd6cf80Smarks int
103ecd6cf80Smarks permset_namecheck(const char *path, namecheck_err_t *why, char *what)
104ecd6cf80Smarks {
105ecd6cf80Smarks 	if (strlen(path) >= ZFS_PERMSET_MAXLEN) {
106ecd6cf80Smarks 		if (why)
107ecd6cf80Smarks 			*why = NAME_ERR_TOOLONG;
108ecd6cf80Smarks 		return (-1);
109ecd6cf80Smarks 	}
110ecd6cf80Smarks 
111ecd6cf80Smarks 	if (path[0] != '@') {
112ecd6cf80Smarks 		if (why) {
113ecd6cf80Smarks 			*why = NAME_ERR_NO_AT;
114ecd6cf80Smarks 			*what = path[0];
115ecd6cf80Smarks 		}
116ecd6cf80Smarks 		return (-1);
117ecd6cf80Smarks 	}
118ecd6cf80Smarks 
11978f17100SMatthew Ahrens 	return (zfs_component_namecheck(&path[1], why, what));
120ecd6cf80Smarks }
121ecd6cf80Smarks 
1221d452cf5Sahrens /*
12323962479SMarcel Telka  * Entity names must be of the following form:
124fa9e4066Sahrens  *
12523962479SMarcel Telka  * 	[component/]*[component][(@|#)component]?
126fa9e4066Sahrens  *
127fa9e4066Sahrens  * Where each component is made up of alphanumeric characters plus the following
128fa9e4066Sahrens  * characters:
129fa9e4066Sahrens  *
130f18faf3fSek110237  * 	[-_.:%]
131f0ed2251Sek110237  *
132f0ed2251Sek110237  * We allow '%' here as we use that character internally to create unique
133f0ed2251Sek110237  * names for temporary clones (for online recv).
134fa9e4066Sahrens  */
135fa9e4066Sahrens int
13623962479SMarcel Telka entity_namecheck(const char *path, namecheck_err_t *why, char *what)
137fa9e4066Sahrens {
13823962479SMarcel Telka 	const char *start, *end;
139fbc66171SMarcel Telka 	int found_delim;
140fa9e4066Sahrens 
141b81d61a6Slling 	/*
142b81d61a6Slling 	 * Make sure the name is not too long.
143b81d61a6Slling 	 */
144ecd6cf80Smarks 
145*40a5c998SMatthew Ahrens 	if (strlen(path) >= ZFS_MAX_DATASET_NAME_LEN) {
146b81d61a6Slling 		if (why)
147b81d61a6Slling 			*why = NAME_ERR_TOOLONG;
148b81d61a6Slling 		return (-1);
149b81d61a6Slling 	}
150b81d61a6Slling 
151fa9e4066Sahrens 	/* Explicitly check for a leading slash.  */
152fa9e4066Sahrens 	if (path[0] == '/') {
153fa9e4066Sahrens 		if (why)
154fa9e4066Sahrens 			*why = NAME_ERR_LEADING_SLASH;
155fa9e4066Sahrens 		return (-1);
156fa9e4066Sahrens 	}
157fa9e4066Sahrens 
158fa9e4066Sahrens 	if (path[0] == '\0') {
159fa9e4066Sahrens 		if (why)
160fa9e4066Sahrens 			*why = NAME_ERR_EMPTY_COMPONENT;
161fa9e4066Sahrens 		return (-1);
162fa9e4066Sahrens 	}
163fa9e4066Sahrens 
164fbc66171SMarcel Telka 	start = path;
165fbc66171SMarcel Telka 	found_delim = 0;
166fa9e4066Sahrens 	for (;;) {
167fa9e4066Sahrens 		/* Find the end of this component */
168fbc66171SMarcel Telka 		end = start;
169fbc66171SMarcel Telka 		while (*end != '/' && *end != '@' && *end != '#' &&
170fbc66171SMarcel Telka 		    *end != '\0')
171fa9e4066Sahrens 			end++;
172fa9e4066Sahrens 
173fa9e4066Sahrens 		if (*end == '\0' && end[-1] == '/') {
174fa9e4066Sahrens 			/* trailing slashes are not allowed */
175fa9e4066Sahrens 			if (why)
176fa9e4066Sahrens 				*why = NAME_ERR_TRAILING_SLASH;
177fa9e4066Sahrens 			return (-1);
178fa9e4066Sahrens 		}
179fa9e4066Sahrens 
180fa9e4066Sahrens 		/* Validate the contents of this component */
18123962479SMarcel Telka 		for (const char *loc = start; loc != end; loc++) {
182f0ed2251Sek110237 			if (!valid_char(*loc) && *loc != '%') {
183fa9e4066Sahrens 				if (why) {
184fa9e4066Sahrens 					*why = NAME_ERR_INVALCHAR;
185fa9e4066Sahrens 					*what = *loc;
186fa9e4066Sahrens 				}
187fa9e4066Sahrens 				return (-1);
188fa9e4066Sahrens 			}
189fbc66171SMarcel Telka 		}
190fbc66171SMarcel Telka 
191fbc66171SMarcel Telka 		/* Snapshot or bookmark delimiter found */
192fbc66171SMarcel Telka 		if (*end == '@' || *end == '#') {
193fbc66171SMarcel Telka 			/* Multiple delimiters are not allowed */
194fbc66171SMarcel Telka 			if (found_delim != 0) {
195fbc66171SMarcel Telka 				if (why)
196fbc66171SMarcel Telka 					*why = NAME_ERR_MULTIPLE_DELIMITERS;
197fbc66171SMarcel Telka 				return (-1);
198fbc66171SMarcel Telka 			}
199fbc66171SMarcel Telka 
200fbc66171SMarcel Telka 			found_delim = 1;
201fbc66171SMarcel Telka 		}
202fbc66171SMarcel Telka 
203fbc66171SMarcel Telka 		/* Zero-length components are not allowed */
204fbc66171SMarcel Telka 		if (start == end) {
205fbc66171SMarcel Telka 			if (why)
206fbc66171SMarcel Telka 				*why = NAME_ERR_EMPTY_COMPONENT;
207fbc66171SMarcel Telka 			return (-1);
208fa9e4066Sahrens 		}
209fa9e4066Sahrens 
210fa9e4066Sahrens 		/* If we've reached the end of the string, we're OK */
211fa9e4066Sahrens 		if (*end == '\0')
212fa9e4066Sahrens 			return (0);
213fa9e4066Sahrens 
214fa9e4066Sahrens 		/*
215fbc66171SMarcel Telka 		 * If there is a '/' in a snapshot or bookmark name
21698579b20Snd150628 		 * then report an error
21798579b20Snd150628 		 */
218fbc66171SMarcel Telka 		if (*end == '/' && found_delim != 0) {
21998579b20Snd150628 			if (why)
22098579b20Snd150628 				*why = NAME_ERR_TRAILING_SLASH;
22198579b20Snd150628 			return (-1);
22298579b20Snd150628 		}
22398579b20Snd150628 
224fa9e4066Sahrens 		/* Update to the next component */
225fbc66171SMarcel Telka 		start = end + 1;
226fa9e4066Sahrens 	}
227fa9e4066Sahrens }
228fa9e4066Sahrens 
22923962479SMarcel Telka /*
23023962479SMarcel Telka  * Dataset is any entity, except bookmark
23123962479SMarcel Telka  */
23223962479SMarcel Telka int
23323962479SMarcel Telka dataset_namecheck(const char *path, namecheck_err_t *why, char *what)
23423962479SMarcel Telka {
23523962479SMarcel Telka 	int ret = entity_namecheck(path, why, what);
23623962479SMarcel Telka 
23723962479SMarcel Telka 	if (ret == 0 && strchr(path, '#') != NULL) {
23823962479SMarcel Telka 		if (why != NULL) {
23923962479SMarcel Telka 			*why = NAME_ERR_INVALCHAR;
24023962479SMarcel Telka 			*what = '#';
24123962479SMarcel Telka 		}
24223962479SMarcel Telka 		return (-1);
24323962479SMarcel Telka 	}
24423962479SMarcel Telka 
24523962479SMarcel Telka 	return (ret);
24623962479SMarcel Telka }
24789eef05eSrm160521 
24889eef05eSrm160521 /*
24989eef05eSrm160521  * mountpoint names must be of the following form:
25089eef05eSrm160521  *
25189eef05eSrm160521  *	/[component][/]*[component][/]
25289eef05eSrm160521  */
25389eef05eSrm160521 int
25489eef05eSrm160521 mountpoint_namecheck(const char *path, namecheck_err_t *why)
25589eef05eSrm160521 {
25689eef05eSrm160521 	const char *start, *end;
25789eef05eSrm160521 
25889eef05eSrm160521 	/*
25989eef05eSrm160521 	 * Make sure none of the mountpoint component names are too long.
26089eef05eSrm160521 	 * If a component name is too long then the mkdir of the mountpoint
26189eef05eSrm160521 	 * will fail but then the mountpoint property will be set to a value
26289eef05eSrm160521 	 * that can never be mounted.  Better to fail before setting the prop.
26389eef05eSrm160521 	 * Extra slashes are OK, they will be tossed by the mountpoint mkdir.
26489eef05eSrm160521 	 */
26589eef05eSrm160521 
26689eef05eSrm160521 	if (path == NULL || *path != '/') {
26789eef05eSrm160521 		if (why)
26889eef05eSrm160521 			*why = NAME_ERR_LEADING_SLASH;
26989eef05eSrm160521 		return (-1);
27089eef05eSrm160521 	}
27189eef05eSrm160521 
27289eef05eSrm160521 	/* Skip leading slash  */
27389eef05eSrm160521 	start = &path[1];
27489eef05eSrm160521 	do {
27589eef05eSrm160521 		end = start;
27689eef05eSrm160521 		while (*end != '/' && *end != '\0')
27789eef05eSrm160521 			end++;
27889eef05eSrm160521 
279*40a5c998SMatthew Ahrens 		if (end - start >= ZFS_MAX_DATASET_NAME_LEN) {
28089eef05eSrm160521 			if (why)
28189eef05eSrm160521 				*why = NAME_ERR_TOOLONG;
28289eef05eSrm160521 			return (-1);
28389eef05eSrm160521 		}
28489eef05eSrm160521 		start = end + 1;
28589eef05eSrm160521 
28689eef05eSrm160521 	} while (*end != '\0');
28789eef05eSrm160521 
28889eef05eSrm160521 	return (0);
28989eef05eSrm160521 }
29089eef05eSrm160521 
291fa9e4066Sahrens /*
292fa9e4066Sahrens  * For pool names, we have the same set of valid characters as described in
293fa9e4066Sahrens  * dataset names, with the additional restriction that the pool name must begin
294fa9e4066Sahrens  * with a letter.  The pool names 'raidz' and 'mirror' are also reserved names
295fa9e4066Sahrens  * that cannot be used.
296fa9e4066Sahrens  */
297fa9e4066Sahrens int
298fa9e4066Sahrens pool_namecheck(const char *pool, namecheck_err_t *why, char *what)
299fa9e4066Sahrens {
300fa9e4066Sahrens 	const char *c;
301fa9e4066Sahrens 
302b81d61a6Slling 	/*
303b81d61a6Slling 	 * Make sure the name is not too long.
304b81d61a6Slling 	 */
305*40a5c998SMatthew Ahrens 	if (strlen(pool) >= ZFS_MAX_DATASET_NAME_LEN) {
306b81d61a6Slling 		if (why)
307b81d61a6Slling 			*why = NAME_ERR_TOOLONG;
308b81d61a6Slling 		return (-1);
309b81d61a6Slling 	}
310b81d61a6Slling 
311fa9e4066Sahrens 	c = pool;
312fa9e4066Sahrens 	while (*c != '\0') {
313fa9e4066Sahrens 		if (!valid_char(*c)) {
314fa9e4066Sahrens 			if (why) {
315fa9e4066Sahrens 				*why = NAME_ERR_INVALCHAR;
316fa9e4066Sahrens 				*what = *c;
317fa9e4066Sahrens 			}
318fa9e4066Sahrens 			return (-1);
319fa9e4066Sahrens 		}
320fa9e4066Sahrens 		c++;
321fa9e4066Sahrens 	}
322fa9e4066Sahrens 
323fa9e4066Sahrens 	if (!(*pool >= 'a' && *pool <= 'z') &&
324fa9e4066Sahrens 	    !(*pool >= 'A' && *pool <= 'Z')) {
325fa9e4066Sahrens 		if (why)
326fa9e4066Sahrens 			*why = NAME_ERR_NOLETTER;
327fa9e4066Sahrens 		return (-1);
328fa9e4066Sahrens 	}
329fa9e4066Sahrens 
330fa9e4066Sahrens 	if (strcmp(pool, "mirror") == 0 || strcmp(pool, "raidz") == 0) {
331fa9e4066Sahrens 		if (why)
332fa9e4066Sahrens 			*why = NAME_ERR_RESERVED;
333fa9e4066Sahrens 		return (-1);
334fa9e4066Sahrens 	}
335fa9e4066Sahrens 
336fa9e4066Sahrens 	if (pool[0] == 'c' && (pool[1] >= '0' && pool[1] <= '9')) {
337fa9e4066Sahrens 		if (why)
338fa9e4066Sahrens 			*why = NAME_ERR_DISKLIKE;
339fa9e4066Sahrens 		return (-1);
340fa9e4066Sahrens 	}
341fa9e4066Sahrens 
342fa9e4066Sahrens 	return (0);
343fa9e4066Sahrens }
344