xref: /titanic_51/usr/src/common/zfs/zfs_namecheck.c (revision f264e9784e7ad23f45f6ce6cc12f9544e7350fcd)
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) >= MAXNAMELEN) {
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  * Dataset 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 dataset_namecheck(const char *path, namecheck_err_t *why, char *what)
137 {
138 	const char *start, *loc, *end;
139 	int found_delim;
140 
141 	/*
142 	 * Make sure the name is not too long.
143 	 *
144 	 * ZFS_MAXNAMELEN is the maximum dataset length used in the userland
145 	 * which is the same as MAXNAMELEN used in the kernel.
146 	 * If ZFS_MAXNAMELEN value is changed, make sure to cleanup all
147 	 * places using MAXNAMELEN.
148 	 */
149 
150 	if (strlen(path) >= MAXNAMELEN) {
151 		if (why)
152 			*why = NAME_ERR_TOOLONG;
153 		return (-1);
154 	}
155 
156 	/* Explicitly check for a leading slash.  */
157 	if (path[0] == '/') {
158 		if (why)
159 			*why = NAME_ERR_LEADING_SLASH;
160 		return (-1);
161 	}
162 
163 	if (path[0] == '\0') {
164 		if (why)
165 			*why = NAME_ERR_EMPTY_COMPONENT;
166 		return (-1);
167 	}
168 
169 	start = path;
170 	found_delim = 0;
171 	for (;;) {
172 		/* Find the end of this component */
173 		end = start;
174 		while (*end != '/' && *end != '@' && *end != '#' &&
175 		    *end != '\0')
176 			end++;
177 
178 		if (*end == '\0' && end[-1] == '/') {
179 			/* trailing slashes are not allowed */
180 			if (why)
181 				*why = NAME_ERR_TRAILING_SLASH;
182 			return (-1);
183 		}
184 
185 		/* Validate the contents of this component */
186 		for (loc = start; loc != end; loc++) {
187 			if (!valid_char(*loc) && *loc != '%') {
188 				if (why) {
189 					*why = NAME_ERR_INVALCHAR;
190 					*what = *loc;
191 				}
192 				return (-1);
193 			}
194 		}
195 
196 		/* Snapshot or bookmark delimiter found */
197 		if (*end == '@' || *end == '#') {
198 			/* Multiple delimiters are not allowed */
199 			if (found_delim != 0) {
200 				if (why)
201 					*why = NAME_ERR_MULTIPLE_DELIMITERS;
202 				return (-1);
203 			}
204 
205 			found_delim = 1;
206 		}
207 
208 		/* Zero-length components are not allowed */
209 		if (start == end) {
210 			if (why)
211 				*why = NAME_ERR_EMPTY_COMPONENT;
212 			return (-1);
213 		}
214 
215 		/* If we've reached the end of the string, we're OK */
216 		if (*end == '\0')
217 			return (0);
218 
219 		/*
220 		 * If there is a '/' in a snapshot or bookmark name
221 		 * then report an error
222 		 */
223 		if (*end == '/' && found_delim != 0) {
224 			if (why)
225 				*why = NAME_ERR_TRAILING_SLASH;
226 			return (-1);
227 		}
228 
229 		/* Update to the next component */
230 		start = end + 1;
231 	}
232 }
233 
234 
235 /*
236  * mountpoint names must be of the following form:
237  *
238  *	/[component][/]*[component][/]
239  */
240 int
241 mountpoint_namecheck(const char *path, namecheck_err_t *why)
242 {
243 	const char *start, *end;
244 
245 	/*
246 	 * Make sure none of the mountpoint component names are too long.
247 	 * If a component name is too long then the mkdir of the mountpoint
248 	 * will fail but then the mountpoint property will be set to a value
249 	 * that can never be mounted.  Better to fail before setting the prop.
250 	 * Extra slashes are OK, they will be tossed by the mountpoint mkdir.
251 	 */
252 
253 	if (path == NULL || *path != '/') {
254 		if (why)
255 			*why = NAME_ERR_LEADING_SLASH;
256 		return (-1);
257 	}
258 
259 	/* Skip leading slash  */
260 	start = &path[1];
261 	do {
262 		end = start;
263 		while (*end != '/' && *end != '\0')
264 			end++;
265 
266 		if (end - start >= MAXNAMELEN) {
267 			if (why)
268 				*why = NAME_ERR_TOOLONG;
269 			return (-1);
270 		}
271 		start = end + 1;
272 
273 	} while (*end != '\0');
274 
275 	return (0);
276 }
277 
278 /*
279  * For pool names, we have the same set of valid characters as described in
280  * dataset names, with the additional restriction that the pool name must begin
281  * with a letter.  The pool names 'raidz' and 'mirror' are also reserved names
282  * that cannot be used.
283  */
284 int
285 pool_namecheck(const char *pool, namecheck_err_t *why, char *what)
286 {
287 	const char *c;
288 
289 	/*
290 	 * Make sure the name is not too long.
291 	 *
292 	 * ZPOOL_MAXNAMELEN is the maximum pool length used in the userland
293 	 * which is the same as MAXNAMELEN used in the kernel.
294 	 * If ZPOOL_MAXNAMELEN value is changed, make sure to cleanup all
295 	 * places using MAXNAMELEN.
296 	 */
297 	if (strlen(pool) >= MAXNAMELEN) {
298 		if (why)
299 			*why = NAME_ERR_TOOLONG;
300 		return (-1);
301 	}
302 
303 	c = pool;
304 	while (*c != '\0') {
305 		if (!valid_char(*c)) {
306 			if (why) {
307 				*why = NAME_ERR_INVALCHAR;
308 				*what = *c;
309 			}
310 			return (-1);
311 		}
312 		c++;
313 	}
314 
315 	if (!(*pool >= 'a' && *pool <= 'z') &&
316 	    !(*pool >= 'A' && *pool <= 'Z')) {
317 		if (why)
318 			*why = NAME_ERR_NOLETTER;
319 		return (-1);
320 	}
321 
322 	if (strcmp(pool, "mirror") == 0 || strcmp(pool, "raidz") == 0) {
323 		if (why)
324 			*why = NAME_ERR_RESERVED;
325 		return (-1);
326 	}
327 
328 	if (pool[0] == 'c' && (pool[1] >= '0' && pool[1] <= '9')) {
329 		if (why)
330 			*why = NAME_ERR_DISKLIKE;
331 		return (-1);
332 	}
333 
334 	return (0);
335 }
336