xref: /titanic_50/usr/src/common/zfs/zfs_namecheck.c (revision 498b7748f0a655972f8ddb28d9c3f518e93cfb5d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * Copyright (c) 2013 by Delphix. All rights reserved.
27  */
28 
29 /*
30  * Common name validation routines for ZFS.  These routines are shared by the
31  * userland code as well as the ioctl() layer to ensure that we don't
32  * inadvertently expose a hole through direct ioctl()s that never gets tested.
33  * In userland, however, we want significantly more information about _why_ the
34  * name is invalid.  In the kernel, we only care whether it's valid or not.
35  * Each routine therefore takes a 'namecheck_err_t' which describes exactly why
36  * the name failed to validate.
37  *
38  * Each function returns 0 on success, -1 on error.
39  */
40 
41 #if defined(_KERNEL)
42 #include <sys/systm.h>
43 #else
44 #include <string.h>
45 #endif
46 
47 #include <sys/param.h>
48 #include <sys/nvpair.h>
49 #include "zfs_namecheck.h"
50 #include "zfs_deleg.h"
51 
52 static int
53 valid_char(char c)
54 {
55 	return ((c >= 'a' && c <= 'z') ||
56 	    (c >= 'A' && c <= 'Z') ||
57 	    (c >= '0' && c <= '9') ||
58 	    c == '-' || c == '_' || c == '.' || c == ':' || c == ' ');
59 }
60 
61 /*
62  * Snapshot names must be made up of alphanumeric characters plus the following
63  * characters:
64  *
65  * 	[-_.: ]
66  */
67 int
68 zfs_component_namecheck(const char *path, namecheck_err_t *why, char *what)
69 {
70 	const char *loc;
71 
72 	if (strlen(path) >= ZFS_MAX_DATASET_NAME_LEN) {
73 		if (why)
74 			*why = NAME_ERR_TOOLONG;
75 		return (-1);
76 	}
77 
78 	if (path[0] == '\0') {
79 		if (why)
80 			*why = NAME_ERR_EMPTY_COMPONENT;
81 		return (-1);
82 	}
83 
84 	for (loc = path; *loc; loc++) {
85 		if (!valid_char(*loc)) {
86 			if (why) {
87 				*why = NAME_ERR_INVALCHAR;
88 				*what = *loc;
89 			}
90 			return (-1);
91 		}
92 	}
93 	return (0);
94 }
95 
96 
97 /*
98  * Permissions set name must start with the letter '@' followed by the
99  * same character restrictions as snapshot names, except that the name
100  * cannot exceed 64 characters.
101  */
102 int
103 permset_namecheck(const char *path, namecheck_err_t *why, char *what)
104 {
105 	if (strlen(path) >= ZFS_PERMSET_MAXLEN) {
106 		if (why)
107 			*why = NAME_ERR_TOOLONG;
108 		return (-1);
109 	}
110 
111 	if (path[0] != '@') {
112 		if (why) {
113 			*why = NAME_ERR_NO_AT;
114 			*what = path[0];
115 		}
116 		return (-1);
117 	}
118 
119 	return (zfs_component_namecheck(&path[1], why, what));
120 }
121 
122 /*
123  * Entity names must be of the following form:
124  *
125  * 	[component/]*[component][(@|#)component]?
126  *
127  * Where each component is made up of alphanumeric characters plus the following
128  * characters:
129  *
130  * 	[-_.:%]
131  *
132  * We allow '%' here as we use that character internally to create unique
133  * names for temporary clones (for online recv).
134  */
135 int
136 entity_namecheck(const char *path, namecheck_err_t *why, char *what)
137 {
138 	const char *start, *end;
139 	int found_delim;
140 
141 	/*
142 	 * Make sure the name is not too long.
143 	 */
144 
145 	if (strlen(path) >= ZFS_MAX_DATASET_NAME_LEN) {
146 		if (why)
147 			*why = NAME_ERR_TOOLONG;
148 		return (-1);
149 	}
150 
151 	/* Explicitly check for a leading slash.  */
152 	if (path[0] == '/') {
153 		if (why)
154 			*why = NAME_ERR_LEADING_SLASH;
155 		return (-1);
156 	}
157 
158 	if (path[0] == '\0') {
159 		if (why)
160 			*why = NAME_ERR_EMPTY_COMPONENT;
161 		return (-1);
162 	}
163 
164 	start = path;
165 	found_delim = 0;
166 	for (;;) {
167 		/* Find the end of this component */
168 		end = start;
169 		while (*end != '/' && *end != '@' && *end != '#' &&
170 		    *end != '\0')
171 			end++;
172 
173 		if (*end == '\0' && end[-1] == '/') {
174 			/* trailing slashes are not allowed */
175 			if (why)
176 				*why = NAME_ERR_TRAILING_SLASH;
177 			return (-1);
178 		}
179 
180 		/* Validate the contents of this component */
181 		for (const char *loc = start; loc != end; loc++) {
182 			if (!valid_char(*loc) && *loc != '%') {
183 				if (why) {
184 					*why = NAME_ERR_INVALCHAR;
185 					*what = *loc;
186 				}
187 				return (-1);
188 			}
189 		}
190 
191 		/* Snapshot or bookmark delimiter found */
192 		if (*end == '@' || *end == '#') {
193 			/* Multiple delimiters are not allowed */
194 			if (found_delim != 0) {
195 				if (why)
196 					*why = NAME_ERR_MULTIPLE_DELIMITERS;
197 				return (-1);
198 			}
199 
200 			found_delim = 1;
201 		}
202 
203 		/* Zero-length components are not allowed */
204 		if (start == end) {
205 			if (why)
206 				*why = NAME_ERR_EMPTY_COMPONENT;
207 			return (-1);
208 		}
209 
210 		/* If we've reached the end of the string, we're OK */
211 		if (*end == '\0')
212 			return (0);
213 
214 		/*
215 		 * If there is a '/' in a snapshot or bookmark name
216 		 * then report an error
217 		 */
218 		if (*end == '/' && found_delim != 0) {
219 			if (why)
220 				*why = NAME_ERR_TRAILING_SLASH;
221 			return (-1);
222 		}
223 
224 		/* Update to the next component */
225 		start = end + 1;
226 	}
227 }
228 
229 /*
230  * Dataset is any entity, except bookmark
231  */
232 int
233 dataset_namecheck(const char *path, namecheck_err_t *why, char *what)
234 {
235 	int ret = entity_namecheck(path, why, what);
236 
237 	if (ret == 0 && strchr(path, '#') != NULL) {
238 		if (why != NULL) {
239 			*why = NAME_ERR_INVALCHAR;
240 			*what = '#';
241 		}
242 		return (-1);
243 	}
244 
245 	return (ret);
246 }
247 
248 /*
249  * mountpoint names must be of the following form:
250  *
251  *	/[component][/]*[component][/]
252  */
253 int
254 mountpoint_namecheck(const char *path, namecheck_err_t *why)
255 {
256 	const char *start, *end;
257 
258 	/*
259 	 * Make sure none of the mountpoint component names are too long.
260 	 * If a component name is too long then the mkdir of the mountpoint
261 	 * will fail but then the mountpoint property will be set to a value
262 	 * that can never be mounted.  Better to fail before setting the prop.
263 	 * Extra slashes are OK, they will be tossed by the mountpoint mkdir.
264 	 */
265 
266 	if (path == NULL || *path != '/') {
267 		if (why)
268 			*why = NAME_ERR_LEADING_SLASH;
269 		return (-1);
270 	}
271 
272 	/* Skip leading slash  */
273 	start = &path[1];
274 	do {
275 		end = start;
276 		while (*end != '/' && *end != '\0')
277 			end++;
278 
279 		if (end - start >= ZFS_MAX_DATASET_NAME_LEN) {
280 			if (why)
281 				*why = NAME_ERR_TOOLONG;
282 			return (-1);
283 		}
284 		start = end + 1;
285 
286 	} while (*end != '\0');
287 
288 	return (0);
289 }
290 
291 /*
292  * For pool names, we have the same set of valid characters as described in
293  * dataset names, with the additional restriction that the pool name must begin
294  * with a letter.  The pool names 'raidz' and 'mirror' are also reserved names
295  * that cannot be used.
296  */
297 int
298 pool_namecheck(const char *pool, namecheck_err_t *why, char *what)
299 {
300 	const char *c;
301 
302 	/*
303 	 * Make sure the name is not too long.
304 	 */
305 	if (strlen(pool) >= ZFS_MAX_DATASET_NAME_LEN) {
306 		if (why)
307 			*why = NAME_ERR_TOOLONG;
308 		return (-1);
309 	}
310 
311 	c = pool;
312 	while (*c != '\0') {
313 		if (!valid_char(*c)) {
314 			if (why) {
315 				*why = NAME_ERR_INVALCHAR;
316 				*what = *c;
317 			}
318 			return (-1);
319 		}
320 		c++;
321 	}
322 
323 	if (!(*pool >= 'a' && *pool <= 'z') &&
324 	    !(*pool >= 'A' && *pool <= 'Z')) {
325 		if (why)
326 			*why = NAME_ERR_NOLETTER;
327 		return (-1);
328 	}
329 
330 	if (strcmp(pool, "mirror") == 0 || strcmp(pool, "raidz") == 0) {
331 		if (why)
332 			*why = NAME_ERR_RESERVED;
333 		return (-1);
334 	}
335 
336 	if (pool[0] == 'c' && (pool[1] >= '0' && pool[1] <= '9')) {
337 		if (why)
338 			*why = NAME_ERR_DISKLIKE;
339 		return (-1);
340 	}
341 
342 	return (0);
343 }
344