zfs_namecheck.c revision 263407
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
52static int
53valid_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 */
67int
68zfs_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 */
102int
103permset_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 */
135int
136dataset_namecheck(const char *path, namecheck_err_t *why, char *what)
137{
138	const char *loc, *end;
139	int found_snapshot;
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	loc = path;
170	found_snapshot = 0;
171	for (;;) {
172		/* Find the end of this component */
173		end = loc;
174		while (*end != '/' && *end != '@' && *end != '\0')
175			end++;
176
177		if (*end == '\0' && end[-1] == '/') {
178			/* trailing slashes are not allowed */
179			if (why)
180				*why = NAME_ERR_TRAILING_SLASH;
181			return (-1);
182		}
183
184		/* Zero-length components are not allowed */
185		if (loc == end) {
186			if (why) {
187				/*
188				 * Make sure this is really a zero-length
189				 * component and not a '@@'.
190				 */
191				if (*end == '@' && found_snapshot) {
192					*why = NAME_ERR_MULTIPLE_AT;
193				} else {
194					*why = NAME_ERR_EMPTY_COMPONENT;
195				}
196			}
197
198			return (-1);
199		}
200
201		/* Validate the contents of this component */
202		while (loc != end) {
203			if (!valid_char(*loc) && *loc != '%') {
204				if (why) {
205					*why = NAME_ERR_INVALCHAR;
206					*what = *loc;
207				}
208				return (-1);
209			}
210			loc++;
211		}
212
213		/* If we've reached the end of the string, we're OK */
214		if (*end == '\0')
215			return (0);
216
217		if (*end == '@') {
218			/*
219			 * If we've found an @ symbol, indicate that we're in
220			 * the snapshot component, and report a second '@'
221			 * character as an error.
222			 */
223			if (found_snapshot) {
224				if (why)
225					*why = NAME_ERR_MULTIPLE_AT;
226				return (-1);
227			}
228
229			found_snapshot = 1;
230		}
231
232		/*
233		 * If there is a '/' in a snapshot name
234		 * then report an error
235		 */
236		if (*end == '/' && found_snapshot) {
237			if (why)
238				*why = NAME_ERR_TRAILING_SLASH;
239			return (-1);
240		}
241
242		/* Update to the next component */
243		loc = end + 1;
244	}
245}
246
247
248/*
249 * mountpoint names must be of the following form:
250 *
251 *	/[component][/]*[component][/]
252 */
253int
254mountpoint_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 >= MAXNAMELEN) {
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 */
297int
298pool_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	 * ZPOOL_MAXNAMELEN is the maximum pool length used in the userland
306	 * which is the same as MAXNAMELEN used in the kernel.
307	 * If ZPOOL_MAXNAMELEN value is changed, make sure to cleanup all
308	 * places using MAXNAMELEN.
309	 */
310	if (strlen(pool) >= MAXNAMELEN) {
311		if (why)
312			*why = NAME_ERR_TOOLONG;
313		return (-1);
314	}
315
316	c = pool;
317	while (*c != '\0') {
318		if (!valid_char(*c)) {
319			if (why) {
320				*why = NAME_ERR_INVALCHAR;
321				*what = *c;
322			}
323			return (-1);
324		}
325		c++;
326	}
327
328	if (!(*pool >= 'a' && *pool <= 'z') &&
329	    !(*pool >= 'A' && *pool <= 'Z')) {
330		if (why)
331			*why = NAME_ERR_NOLETTER;
332		return (-1);
333	}
334
335	if (strcmp(pool, "mirror") == 0 || strcmp(pool, "raidz") == 0) {
336		if (why)
337			*why = NAME_ERR_RESERVED;
338		return (-1);
339	}
340
341	if (pool[0] == 'c' && (pool[1] >= '0' && pool[1] <= '9')) {
342		if (why)
343			*why = NAME_ERR_DISKLIKE;
344		return (-1);
345	}
346
347	return (0);
348}
349