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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 * Copyright (c) 2013, 2016 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.
43 #include <sys/dsl_dir.h>
44 #include <sys/param.h>
45 #include <sys/nvpair.h>
46 #include "zfs_namecheck.h"
47 #include "zfs_deleg.h"
50 * Deeply nested datasets can overflow the stack, so we put a limit
51 * in the amount of nesting a path can have. zfs_max_dataset_nesting
52 * can be tuned temporarily to fix existing datasets that exceed our
55 int zfs_max_dataset_nesting = 50;
60 return ((c >= 'a' && c <= 'z') ||
61 (c >= 'A' && c <= 'Z') ||
62 (c >= '0' && c <= '9') ||
63 c == '-' || c == '_' || c == '.' || c == ':' || c == ' ');
67 * Looks at a path and returns its level of nesting (depth).
70 get_dataset_depth(const char *path)
72 const char *loc = path;
76 * Keep track of nesting until you hit the end of the
77 * path or found the snapshot/bookmark separator.
79 for (int i = 0; loc[i] != '\0' &&
90 * Snapshot names must be made up of alphanumeric characters plus the following
95 * Returns 0 on success, -1 on error.
98 zfs_component_namecheck(const char *path, namecheck_err_t *why, char *what)
102 if (strlen(path) >= ZFS_MAX_DATASET_NAME_LEN) {
104 *why = NAME_ERR_TOOLONG;
108 if (path[0] == '\0') {
110 *why = NAME_ERR_EMPTY_COMPONENT;
114 for (loc = path; *loc; loc++) {
115 if (!valid_char(*loc)) {
117 *why = NAME_ERR_INVALCHAR;
128 * Permissions set name must start with the letter '@' followed by the
129 * same character restrictions as snapshot names, except that the name
130 * cannot exceed 64 characters.
132 * Returns 0 on success, -1 on error.
135 permset_namecheck(const char *path, namecheck_err_t *why, char *what)
137 if (strlen(path) >= ZFS_PERMSET_MAXLEN) {
139 *why = NAME_ERR_TOOLONG;
143 if (path[0] != '@') {
145 *why = NAME_ERR_NO_AT;
151 return (zfs_component_namecheck(&path[1], why, what));
155 * Dataset paths should not be deeper than zfs_max_dataset_nesting
156 * in terms of nesting.
158 * Returns 0 on success, -1 on error.
161 dataset_nestcheck(const char *path)
163 return ((get_dataset_depth(path) < zfs_max_dataset_nesting) ? 0 : -1);
167 * Entity names must be of the following form:
169 * [component/]*[component][(@|#)component]?
171 * Where each component is made up of alphanumeric characters plus the following
176 * We allow '%' here as we use that character internally to create unique
177 * names for temporary clones (for online recv).
179 * Returns 0 on success, -1 on error.
182 entity_namecheck(const char *path, namecheck_err_t *why, char *what)
186 EQUIV(why == NULL, what == NULL);
189 * Make sure the name is not too long.
191 if (strlen(path) >= ZFS_MAX_DATASET_NAME_LEN) {
193 *why = NAME_ERR_TOOLONG;
197 /* Explicitly check for a leading slash. */
198 if (path[0] == '/') {
200 *why = NAME_ERR_LEADING_SLASH;
204 if (path[0] == '\0') {
206 *why = NAME_ERR_EMPTY_COMPONENT;
210 const char *start = path;
211 boolean_t found_delim = B_FALSE;
213 /* Find the end of this component */
215 while (*end != '/' && *end != '@' && *end != '#' &&
219 if (*end == '\0' && end[-1] == '/') {
220 /* trailing slashes are not allowed */
222 *why = NAME_ERR_TRAILING_SLASH;
226 /* Validate the contents of this component */
227 for (const char *loc = start; loc != end; loc++) {
228 if (!valid_char(*loc) && *loc != '%') {
230 *why = NAME_ERR_INVALCHAR;
237 if (*end == '\0' || *end == '/') {
238 int component_length = end - start;
239 /* Validate the contents of this component is not '.' */
240 if (component_length == 1) {
241 if (start[0] == '.') {
243 *why = NAME_ERR_SELF_REF;
248 /* Validate the content of this component is not '..' */
249 if (component_length == 2) {
250 if (start[0] == '.' && start[1] == '.') {
252 *why = NAME_ERR_PARENT_REF;
258 /* Snapshot or bookmark delimiter found */
259 if (*end == '@' || *end == '#') {
260 /* Multiple delimiters are not allowed */
261 if (found_delim != 0) {
263 *why = NAME_ERR_MULTIPLE_DELIMITERS;
267 found_delim = B_TRUE;
270 /* Zero-length components are not allowed */
273 *why = NAME_ERR_EMPTY_COMPONENT;
277 /* If we've reached the end of the string, we're OK */
282 * If there is a '/' in a snapshot or bookmark name
283 * then report an error
285 if (*end == '/' && found_delim != 0) {
287 *why = NAME_ERR_TRAILING_SLASH;
291 /* Update to the next component */
297 * Dataset is any entity, except bookmark
300 dataset_namecheck(const char *path, namecheck_err_t *why, char *what)
302 int ret = entity_namecheck(path, why, what);
304 if (ret == 0 && strchr(path, '#') != NULL) {
306 *why = NAME_ERR_INVALCHAR;
316 * Assert path is a valid bookmark name
319 bookmark_namecheck(const char *path, namecheck_err_t *why, char *what)
321 int ret = entity_namecheck(path, why, what);
323 if (ret == 0 && strchr(path, '#') == NULL) {
325 *why = NAME_ERR_NO_POUND;
335 * Assert path is a valid snapshot name
338 snapshot_namecheck(const char *path, namecheck_err_t *why, char *what)
340 int ret = entity_namecheck(path, why, what);
342 if (ret == 0 && strchr(path, '@') == NULL) {
344 *why = NAME_ERR_NO_AT;
354 * mountpoint names must be of the following form:
356 * /[component][/]*[component][/]
358 * Returns 0 on success, -1 on error.
361 mountpoint_namecheck(const char *path, namecheck_err_t *why)
363 const char *start, *end;
366 * Make sure none of the mountpoint component names are too long.
367 * If a component name is too long then the mkdir of the mountpoint
368 * will fail but then the mountpoint property will be set to a value
369 * that can never be mounted. Better to fail before setting the prop.
370 * Extra slashes are OK, they will be tossed by the mountpoint mkdir.
373 if (path == NULL || *path != '/') {
375 *why = NAME_ERR_LEADING_SLASH;
379 /* Skip leading slash */
383 while (*end != '/' && *end != '\0')
386 if (end - start >= ZFS_MAX_DATASET_NAME_LEN) {
388 *why = NAME_ERR_TOOLONG;
393 } while (*end != '\0');
399 * For pool names, we have the same set of valid characters as described in
400 * dataset names, with the additional restriction that the pool name must begin
401 * with a letter. The pool names 'raidz' and 'mirror' are also reserved names
402 * that cannot be used.
404 * Returns 0 on success, -1 on error.
407 pool_namecheck(const char *pool, namecheck_err_t *why, char *what)
412 * Make sure the name is not too long.
413 * If we're creating a pool with version >= SPA_VERSION_DSL_SCRUB (v11)
414 * we need to account for additional space needed by the origin ds which
415 * will also be snapshotted: "poolname"+"/"+"$ORIGIN"+"@"+"$ORIGIN".
416 * Play it safe and enforce this limit even if the pool version is < 11
417 * so it can be upgraded without issues.
419 if (strlen(pool) >= (ZFS_MAX_DATASET_NAME_LEN - 2 -
420 strlen(ORIGIN_DIR_NAME) * 2)) {
422 *why = NAME_ERR_TOOLONG;
428 if (!valid_char(*c)) {
430 *why = NAME_ERR_INVALCHAR;
438 if (!(*pool >= 'a' && *pool <= 'z') &&
439 !(*pool >= 'A' && *pool <= 'Z')) {
441 *why = NAME_ERR_NOLETTER;
445 if (strcmp(pool, "mirror") == 0 || strcmp(pool, "raidz") == 0) {
447 *why = NAME_ERR_RESERVED;
451 if (pool[0] == 'c' && (pool[1] >= '0' && pool[1] <= '9')) {
453 *why = NAME_ERR_DISKLIKE;
460 EXPORT_SYMBOL(entity_namecheck);
461 EXPORT_SYMBOL(pool_namecheck);
462 EXPORT_SYMBOL(dataset_namecheck);
463 EXPORT_SYMBOL(bookmark_namecheck);
464 EXPORT_SYMBOL(snapshot_namecheck);
465 EXPORT_SYMBOL(zfs_component_namecheck);
466 EXPORT_SYMBOL(dataset_nestcheck);
467 EXPORT_SYMBOL(get_dataset_depth);
468 EXPORT_SYMBOL(zfs_max_dataset_nesting);
470 ZFS_MODULE_PARAM(zfs, zfs_, max_dataset_nesting, INT, ZMOD_RW,
471 "Limit to the amount of nesting a path can have. Defaults to 50.");