Merge commit '720b16875295d57e0e6a4e0ec32db4d47412f896'
[unleashed.git] / kernel / fs / zfs / common / zfs_namecheck.c
blob09975125261bfc55fe0269eed5f6436aefdd989f
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 * Copyright (c) 2013 by Delphix. All rights reserved.
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.
38 * Each function returns 0 on success, -1 on error.
41 #if defined(_KERNEL)
42 #include <sys/systm.h>
43 #else
44 #include <string.h>
45 #endif
47 #include <sys/dsl_dir.h>
48 #include <sys/param.h>
49 #include <sys/nvpair.h>
50 #include "zfs_namecheck.h"
51 #include "zfs_deleg.h"
53 static int
54 valid_char(char c)
56 return ((c >= 'a' && c <= 'z') ||
57 (c >= 'A' && c <= 'Z') ||
58 (c >= '0' && c <= '9') ||
59 c == '-' || c == '_' || c == '.' || c == ':' || c == ' ');
63 * Snapshot names must be made up of alphanumeric characters plus the following
64 * characters:
66 * [-_.: ]
68 int
69 zfs_component_namecheck(const char *path, namecheck_err_t *why, char *what)
71 const char *loc;
73 if (strlen(path) >= ZFS_MAX_DATASET_NAME_LEN) {
74 if (why)
75 *why = NAME_ERR_TOOLONG;
76 return (-1);
79 if (path[0] == '\0') {
80 if (why)
81 *why = NAME_ERR_EMPTY_COMPONENT;
82 return (-1);
85 for (loc = path; *loc; loc++) {
86 if (!valid_char(*loc)) {
87 if (why) {
88 *why = NAME_ERR_INVALCHAR;
89 *what = *loc;
91 return (-1);
94 return (0);
99 * Permissions set name must start with the letter '@' followed by the
100 * same character restrictions as snapshot names, except that the name
101 * cannot exceed 64 characters.
104 permset_namecheck(const char *path, namecheck_err_t *why, char *what)
106 if (strlen(path) >= ZFS_PERMSET_MAXLEN) {
107 if (why)
108 *why = NAME_ERR_TOOLONG;
109 return (-1);
112 if (path[0] != '@') {
113 if (why) {
114 *why = NAME_ERR_NO_AT;
115 *what = path[0];
117 return (-1);
120 return (zfs_component_namecheck(&path[1], why, what));
124 * Entity names must be of the following form:
126 * [component/]*[component][(@|#)component]?
128 * Where each component is made up of alphanumeric characters plus the following
129 * characters:
131 * [-_.:%]
133 * We allow '%' here as we use that character internally to create unique
134 * names for temporary clones (for online recv).
137 entity_namecheck(const char *path, namecheck_err_t *why, char *what)
139 const char *start, *end;
140 int found_delim;
143 * Make sure the name is not too long.
146 if (strlen(path) >= ZFS_MAX_DATASET_NAME_LEN) {
147 if (why)
148 *why = NAME_ERR_TOOLONG;
149 return (-1);
152 /* Explicitly check for a leading slash. */
153 if (path[0] == '/') {
154 if (why)
155 *why = NAME_ERR_LEADING_SLASH;
156 return (-1);
159 if (path[0] == '\0') {
160 if (why)
161 *why = NAME_ERR_EMPTY_COMPONENT;
162 return (-1);
165 start = path;
166 found_delim = 0;
167 for (;;) {
168 /* Find the end of this component */
169 end = start;
170 while (*end != '/' && *end != '@' && *end != '#' &&
171 *end != '\0')
172 end++;
174 if (*end == '\0' && end[-1] == '/') {
175 /* trailing slashes are not allowed */
176 if (why)
177 *why = NAME_ERR_TRAILING_SLASH;
178 return (-1);
181 /* Validate the contents of this component */
182 for (const char *loc = start; loc != end; loc++) {
183 if (!valid_char(*loc) && *loc != '%') {
184 if (why) {
185 *why = NAME_ERR_INVALCHAR;
186 *what = *loc;
188 return (-1);
192 /* Snapshot or bookmark delimiter found */
193 if (*end == '@' || *end == '#') {
194 /* Multiple delimiters are not allowed */
195 if (found_delim != 0) {
196 if (why)
197 *why = NAME_ERR_MULTIPLE_DELIMITERS;
198 return (-1);
201 found_delim = 1;
204 /* Zero-length components are not allowed */
205 if (start == end) {
206 if (why)
207 *why = NAME_ERR_EMPTY_COMPONENT;
208 return (-1);
211 /* If we've reached the end of the string, we're OK */
212 if (*end == '\0')
213 return (0);
216 * If there is a '/' in a snapshot or bookmark name
217 * then report an error
219 if (*end == '/' && found_delim != 0) {
220 if (why)
221 *why = NAME_ERR_TRAILING_SLASH;
222 return (-1);
225 /* Update to the next component */
226 start = end + 1;
231 * Dataset is any entity, except bookmark
234 dataset_namecheck(const char *path, namecheck_err_t *why, char *what)
236 int ret = entity_namecheck(path, why, what);
238 if (ret == 0 && strchr(path, '#') != NULL) {
239 if (why != NULL) {
240 *why = NAME_ERR_INVALCHAR;
241 *what = '#';
243 return (-1);
246 return (ret);
250 * mountpoint names must be of the following form:
252 * /[component][/]*[component][/]
255 mountpoint_namecheck(const char *path, namecheck_err_t *why)
257 const char *start, *end;
260 * Make sure none of the mountpoint component names are too long.
261 * If a component name is too long then the mkdir of the mountpoint
262 * will fail but then the mountpoint property will be set to a value
263 * that can never be mounted. Better to fail before setting the prop.
264 * Extra slashes are OK, they will be tossed by the mountpoint mkdir.
267 if (path == NULL || *path != '/') {
268 if (why)
269 *why = NAME_ERR_LEADING_SLASH;
270 return (-1);
273 /* Skip leading slash */
274 start = &path[1];
275 do {
276 end = start;
277 while (*end != '/' && *end != '\0')
278 end++;
280 if (end - start >= ZFS_MAX_DATASET_NAME_LEN) {
281 if (why)
282 *why = NAME_ERR_TOOLONG;
283 return (-1);
285 start = end + 1;
287 } while (*end != '\0');
289 return (0);
293 * For pool names, we have the same set of valid characters as described in
294 * dataset names, with the additional restriction that the pool name must begin
295 * with a letter. The pool names 'raidz' and 'mirror' are also reserved names
296 * that cannot be used.
299 pool_namecheck(const char *pool, namecheck_err_t *why, char *what)
301 const char *c;
304 * Make sure the name is not too long.
305 * If we're creating a pool with version >= SPA_VERSION_DSL_SCRUB (v11)
306 * we need to account for additional space needed by the origin ds which
307 * will also be snapshotted: "poolname"+"/"+"$ORIGIN"+"@"+"$ORIGIN".
308 * Play it safe and enforce this limit even if the pool version is < 11
309 * so it can be upgraded without issues.
311 if (strlen(pool) >= (ZFS_MAX_DATASET_NAME_LEN - 2 -
312 strlen(ORIGIN_DIR_NAME) * 2)) {
313 if (why)
314 *why = NAME_ERR_TOOLONG;
315 return (-1);
318 c = pool;
319 while (*c != '\0') {
320 if (!valid_char(*c)) {
321 if (why) {
322 *why = NAME_ERR_INVALCHAR;
323 *what = *c;
325 return (-1);
327 c++;
330 if (!(*pool >= 'a' && *pool <= 'z') &&
331 !(*pool >= 'A' && *pool <= 'Z')) {
332 if (why)
333 *why = NAME_ERR_NOLETTER;
334 return (-1);
337 if (strcmp(pool, "mirror") == 0 || strcmp(pool, "raidz") == 0) {
338 if (why)
339 *why = NAME_ERR_RESERVED;
340 return (-1);
343 if (pool[0] == 'c' && (pool[1] >= '0' && pool[1] <= '9')) {
344 if (why)
345 *why = NAME_ERR_DISKLIKE;
346 return (-1);
349 return (0);