xref: /titanic_53/usr/src/common/zfs/zfs_namecheck.c (revision 239624797d44f53fe395a44e6dac0bdba7b2b8b8)
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 
721d452cf5Sahrens 	if (strlen(path) >= MAXNAMELEN) {
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 /*
123*23962479SMarcel Telka  * Entity names must be of the following form:
124fa9e4066Sahrens  *
125*23962479SMarcel 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
136*23962479SMarcel Telka entity_namecheck(const char *path, namecheck_err_t *why, char *what)
137fa9e4066Sahrens {
138*23962479SMarcel Telka 	const char *start, *end;
139fbc66171SMarcel Telka 	int found_delim;
140fa9e4066Sahrens 
141b81d61a6Slling 	/*
142b81d61a6Slling 	 * Make sure the name is not too long.
143b81d61a6Slling 	 *
144b81d61a6Slling 	 * ZFS_MAXNAMELEN is the maximum dataset length used in the userland
145b81d61a6Slling 	 * which is the same as MAXNAMELEN used in the kernel.
146b81d61a6Slling 	 * If ZFS_MAXNAMELEN value is changed, make sure to cleanup all
147b81d61a6Slling 	 * places using MAXNAMELEN.
148b81d61a6Slling 	 */
149ecd6cf80Smarks 
150b81d61a6Slling 	if (strlen(path) >= MAXNAMELEN) {
151b81d61a6Slling 		if (why)
152b81d61a6Slling 			*why = NAME_ERR_TOOLONG;
153b81d61a6Slling 		return (-1);
154b81d61a6Slling 	}
155b81d61a6Slling 
156fa9e4066Sahrens 	/* Explicitly check for a leading slash.  */
157fa9e4066Sahrens 	if (path[0] == '/') {
158fa9e4066Sahrens 		if (why)
159fa9e4066Sahrens 			*why = NAME_ERR_LEADING_SLASH;
160fa9e4066Sahrens 		return (-1);
161fa9e4066Sahrens 	}
162fa9e4066Sahrens 
163fa9e4066Sahrens 	if (path[0] == '\0') {
164fa9e4066Sahrens 		if (why)
165fa9e4066Sahrens 			*why = NAME_ERR_EMPTY_COMPONENT;
166fa9e4066Sahrens 		return (-1);
167fa9e4066Sahrens 	}
168fa9e4066Sahrens 
169fbc66171SMarcel Telka 	start = path;
170fbc66171SMarcel Telka 	found_delim = 0;
171fa9e4066Sahrens 	for (;;) {
172fa9e4066Sahrens 		/* Find the end of this component */
173fbc66171SMarcel Telka 		end = start;
174fbc66171SMarcel Telka 		while (*end != '/' && *end != '@' && *end != '#' &&
175fbc66171SMarcel Telka 		    *end != '\0')
176fa9e4066Sahrens 			end++;
177fa9e4066Sahrens 
178fa9e4066Sahrens 		if (*end == '\0' && end[-1] == '/') {
179fa9e4066Sahrens 			/* trailing slashes are not allowed */
180fa9e4066Sahrens 			if (why)
181fa9e4066Sahrens 				*why = NAME_ERR_TRAILING_SLASH;
182fa9e4066Sahrens 			return (-1);
183fa9e4066Sahrens 		}
184fa9e4066Sahrens 
185fa9e4066Sahrens 		/* Validate the contents of this component */
186*23962479SMarcel Telka 		for (const char *loc = start; loc != end; loc++) {
187f0ed2251Sek110237 			if (!valid_char(*loc) && *loc != '%') {
188fa9e4066Sahrens 				if (why) {
189fa9e4066Sahrens 					*why = NAME_ERR_INVALCHAR;
190fa9e4066Sahrens 					*what = *loc;
191fa9e4066Sahrens 				}
192fa9e4066Sahrens 				return (-1);
193fa9e4066Sahrens 			}
194fbc66171SMarcel Telka 		}
195fbc66171SMarcel Telka 
196fbc66171SMarcel Telka 		/* Snapshot or bookmark delimiter found */
197fbc66171SMarcel Telka 		if (*end == '@' || *end == '#') {
198fbc66171SMarcel Telka 			/* Multiple delimiters are not allowed */
199fbc66171SMarcel Telka 			if (found_delim != 0) {
200fbc66171SMarcel Telka 				if (why)
201fbc66171SMarcel Telka 					*why = NAME_ERR_MULTIPLE_DELIMITERS;
202fbc66171SMarcel Telka 				return (-1);
203fbc66171SMarcel Telka 			}
204fbc66171SMarcel Telka 
205fbc66171SMarcel Telka 			found_delim = 1;
206fbc66171SMarcel Telka 		}
207fbc66171SMarcel Telka 
208fbc66171SMarcel Telka 		/* Zero-length components are not allowed */
209fbc66171SMarcel Telka 		if (start == end) {
210fbc66171SMarcel Telka 			if (why)
211fbc66171SMarcel Telka 				*why = NAME_ERR_EMPTY_COMPONENT;
212fbc66171SMarcel Telka 			return (-1);
213fa9e4066Sahrens 		}
214fa9e4066Sahrens 
215fa9e4066Sahrens 		/* If we've reached the end of the string, we're OK */
216fa9e4066Sahrens 		if (*end == '\0')
217fa9e4066Sahrens 			return (0);
218fa9e4066Sahrens 
219fa9e4066Sahrens 		/*
220fbc66171SMarcel Telka 		 * If there is a '/' in a snapshot or bookmark name
22198579b20Snd150628 		 * then report an error
22298579b20Snd150628 		 */
223fbc66171SMarcel Telka 		if (*end == '/' && found_delim != 0) {
22498579b20Snd150628 			if (why)
22598579b20Snd150628 				*why = NAME_ERR_TRAILING_SLASH;
22698579b20Snd150628 			return (-1);
22798579b20Snd150628 		}
22898579b20Snd150628 
229fa9e4066Sahrens 		/* Update to the next component */
230fbc66171SMarcel Telka 		start = end + 1;
231fa9e4066Sahrens 	}
232fa9e4066Sahrens }
233fa9e4066Sahrens 
234*23962479SMarcel Telka /*
235*23962479SMarcel Telka  * Dataset is any entity, except bookmark
236*23962479SMarcel Telka  */
237*23962479SMarcel Telka int
238*23962479SMarcel Telka dataset_namecheck(const char *path, namecheck_err_t *why, char *what)
239*23962479SMarcel Telka {
240*23962479SMarcel Telka 	int ret = entity_namecheck(path, why, what);
241*23962479SMarcel Telka 
242*23962479SMarcel Telka 	if (ret == 0 && strchr(path, '#') != NULL) {
243*23962479SMarcel Telka 		if (why != NULL) {
244*23962479SMarcel Telka 			*why = NAME_ERR_INVALCHAR;
245*23962479SMarcel Telka 			*what = '#';
246*23962479SMarcel Telka 		}
247*23962479SMarcel Telka 		return (-1);
248*23962479SMarcel Telka 	}
249*23962479SMarcel Telka 
250*23962479SMarcel Telka 	return (ret);
251*23962479SMarcel Telka }
25289eef05eSrm160521 
25389eef05eSrm160521 /*
25489eef05eSrm160521  * mountpoint names must be of the following form:
25589eef05eSrm160521  *
25689eef05eSrm160521  *	/[component][/]*[component][/]
25789eef05eSrm160521  */
25889eef05eSrm160521 int
25989eef05eSrm160521 mountpoint_namecheck(const char *path, namecheck_err_t *why)
26089eef05eSrm160521 {
26189eef05eSrm160521 	const char *start, *end;
26289eef05eSrm160521 
26389eef05eSrm160521 	/*
26489eef05eSrm160521 	 * Make sure none of the mountpoint component names are too long.
26589eef05eSrm160521 	 * If a component name is too long then the mkdir of the mountpoint
26689eef05eSrm160521 	 * will fail but then the mountpoint property will be set to a value
26789eef05eSrm160521 	 * that can never be mounted.  Better to fail before setting the prop.
26889eef05eSrm160521 	 * Extra slashes are OK, they will be tossed by the mountpoint mkdir.
26989eef05eSrm160521 	 */
27089eef05eSrm160521 
27189eef05eSrm160521 	if (path == NULL || *path != '/') {
27289eef05eSrm160521 		if (why)
27389eef05eSrm160521 			*why = NAME_ERR_LEADING_SLASH;
27489eef05eSrm160521 		return (-1);
27589eef05eSrm160521 	}
27689eef05eSrm160521 
27789eef05eSrm160521 	/* Skip leading slash  */
27889eef05eSrm160521 	start = &path[1];
27989eef05eSrm160521 	do {
28089eef05eSrm160521 		end = start;
28189eef05eSrm160521 		while (*end != '/' && *end != '\0')
28289eef05eSrm160521 			end++;
28389eef05eSrm160521 
28489eef05eSrm160521 		if (end - start >= MAXNAMELEN) {
28589eef05eSrm160521 			if (why)
28689eef05eSrm160521 				*why = NAME_ERR_TOOLONG;
28789eef05eSrm160521 			return (-1);
28889eef05eSrm160521 		}
28989eef05eSrm160521 		start = end + 1;
29089eef05eSrm160521 
29189eef05eSrm160521 	} while (*end != '\0');
29289eef05eSrm160521 
29389eef05eSrm160521 	return (0);
29489eef05eSrm160521 }
29589eef05eSrm160521 
296fa9e4066Sahrens /*
297fa9e4066Sahrens  * For pool names, we have the same set of valid characters as described in
298fa9e4066Sahrens  * dataset names, with the additional restriction that the pool name must begin
299fa9e4066Sahrens  * with a letter.  The pool names 'raidz' and 'mirror' are also reserved names
300fa9e4066Sahrens  * that cannot be used.
301fa9e4066Sahrens  */
302fa9e4066Sahrens int
303fa9e4066Sahrens pool_namecheck(const char *pool, namecheck_err_t *why, char *what)
304fa9e4066Sahrens {
305fa9e4066Sahrens 	const char *c;
306fa9e4066Sahrens 
307b81d61a6Slling 	/*
308b81d61a6Slling 	 * Make sure the name is not too long.
309b81d61a6Slling 	 *
310b81d61a6Slling 	 * ZPOOL_MAXNAMELEN is the maximum pool length used in the userland
311b81d61a6Slling 	 * which is the same as MAXNAMELEN used in the kernel.
312b81d61a6Slling 	 * If ZPOOL_MAXNAMELEN value is changed, make sure to cleanup all
313b81d61a6Slling 	 * places using MAXNAMELEN.
314b81d61a6Slling 	 */
315b81d61a6Slling 	if (strlen(pool) >= MAXNAMELEN) {
316b81d61a6Slling 		if (why)
317b81d61a6Slling 			*why = NAME_ERR_TOOLONG;
318b81d61a6Slling 		return (-1);
319b81d61a6Slling 	}
320b81d61a6Slling 
321fa9e4066Sahrens 	c = pool;
322fa9e4066Sahrens 	while (*c != '\0') {
323fa9e4066Sahrens 		if (!valid_char(*c)) {
324fa9e4066Sahrens 			if (why) {
325fa9e4066Sahrens 				*why = NAME_ERR_INVALCHAR;
326fa9e4066Sahrens 				*what = *c;
327fa9e4066Sahrens 			}
328fa9e4066Sahrens 			return (-1);
329fa9e4066Sahrens 		}
330fa9e4066Sahrens 		c++;
331fa9e4066Sahrens 	}
332fa9e4066Sahrens 
333fa9e4066Sahrens 	if (!(*pool >= 'a' && *pool <= 'z') &&
334fa9e4066Sahrens 	    !(*pool >= 'A' && *pool <= 'Z')) {
335fa9e4066Sahrens 		if (why)
336fa9e4066Sahrens 			*why = NAME_ERR_NOLETTER;
337fa9e4066Sahrens 		return (-1);
338fa9e4066Sahrens 	}
339fa9e4066Sahrens 
340fa9e4066Sahrens 	if (strcmp(pool, "mirror") == 0 || strcmp(pool, "raidz") == 0) {
341fa9e4066Sahrens 		if (why)
342fa9e4066Sahrens 			*why = NAME_ERR_RESERVED;
343fa9e4066Sahrens 		return (-1);
344fa9e4066Sahrens 	}
345fa9e4066Sahrens 
346fa9e4066Sahrens 	if (pool[0] == 'c' && (pool[1] >= '0' && pool[1] <= '9')) {
347fa9e4066Sahrens 		if (why)
348fa9e4066Sahrens 			*why = NAME_ERR_DISKLIKE;
349fa9e4066Sahrens 		return (-1);
350fa9e4066Sahrens 	}
351fa9e4066Sahrens 
352fa9e4066Sahrens 	return (0);
353fa9e4066Sahrens }
354