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.
40 #include <sys/systm.h>
45 #include <sys/dsl_dir.h>
46 #include <sys/param.h>
47 #include <sys/nvpair.h>
48 #include "zfs_namecheck.h"
49 #include "zfs_deleg.h"
52 * Deeply nested datasets can overflow the stack, so we put a limit
53 * in the amount of nesting a path can have. zfs_max_dataset_nesting
54 * can be tuned temporarily to fix existing datasets that exceed our
57 int zfs_max_dataset_nesting = 50;
62 return ((c >= 'a' && c <= 'z') ||
63 (c >= 'A' && c <= 'Z') ||
64 (c >= '0' && c <= '9') ||
65 c == '-' || c == '_' || c == '.' || c == ':' || c == ' ');
69 * Looks at a path and returns its level of nesting (depth).
72 get_dataset_depth(const char *path)
74 const char *loc = path;
78 * Keep track of nesting until you hit the end of the
79 * path or found the snapshot/bookmark seperator.
81 for (int i = 0; loc[i] != '\0' &&
92 * Snapshot names must be made up of alphanumeric characters plus the following
97 * Returns 0 on success, -1 on error.
100 zfs_component_namecheck(const char *path, namecheck_err_t *why, char *what)
104 if (strlen(path) >= ZFS_MAX_DATASET_NAME_LEN) {
106 *why = NAME_ERR_TOOLONG;
110 if (path[0] == '\0') {
112 *why = NAME_ERR_EMPTY_COMPONENT;
116 for (loc = path; *loc; loc++) {
117 if (!valid_char(*loc)) {
119 *why = NAME_ERR_INVALCHAR;
130 * Permissions set name must start with the letter '@' followed by the
131 * same character restrictions as snapshot names, except that the name
132 * cannot exceed 64 characters.
134 * Returns 0 on success, -1 on error.
137 permset_namecheck(const char *path, namecheck_err_t *why, char *what)
139 if (strlen(path) >= ZFS_PERMSET_MAXLEN) {
141 *why = NAME_ERR_TOOLONG;
145 if (path[0] != '@') {
147 *why = NAME_ERR_NO_AT;
153 return (zfs_component_namecheck(&path[1], why, what));
157 * Dataset paths should not be deeper than zfs_max_dataset_nesting
158 * in terms of nesting.
160 * Returns 0 on success, -1 on error.
163 dataset_nestcheck(const char *path)
165 return ((get_dataset_depth(path) < zfs_max_dataset_nesting) ? 0 : -1);
169 * Entity names must be of the following form:
171 * [component/]*[component][(@|#)component]?
173 * Where each component is made up of alphanumeric characters plus the following
178 * We allow '%' here as we use that character internally to create unique
179 * names for temporary clones (for online recv).
181 * Returns 0 on success, -1 on error.
184 entity_namecheck(const char *path, namecheck_err_t *why, char *what)
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 /* Snapshot or bookmark delimiter found */
238 if (*end == '@' || *end == '#') {
239 /* Multiple delimiters are not allowed */
240 if (found_delim != 0) {
242 *why = NAME_ERR_MULTIPLE_DELIMITERS;
246 found_delim = B_TRUE;
249 /* Zero-length components are not allowed */
252 *why = NAME_ERR_EMPTY_COMPONENT;
256 /* If we've reached the end of the string, we're OK */
261 * If there is a '/' in a snapshot or bookmark name
262 * then report an error
264 if (*end == '/' && found_delim != 0) {
266 *why = NAME_ERR_TRAILING_SLASH;
270 /* Update to the next component */
276 * Dataset is any entity, except bookmark
279 dataset_namecheck(const char *path, namecheck_err_t *why, char *what)
281 int ret = entity_namecheck(path, why, what);
283 if (ret == 0 && strchr(path, '#') != NULL) {
285 *why = NAME_ERR_INVALCHAR;
295 * mountpoint names must be of the following form:
297 * /[component][/]*[component][/]
299 * Returns 0 on success, -1 on error.
302 mountpoint_namecheck(const char *path, namecheck_err_t *why)
304 const char *start, *end;
307 * Make sure none of the mountpoint component names are too long.
308 * If a component name is too long then the mkdir of the mountpoint
309 * will fail but then the mountpoint property will be set to a value
310 * that can never be mounted. Better to fail before setting the prop.
311 * Extra slashes are OK, they will be tossed by the mountpoint mkdir.
314 if (path == NULL || *path != '/') {
316 *why = NAME_ERR_LEADING_SLASH;
320 /* Skip leading slash */
324 while (*end != '/' && *end != '\0')
327 if (end - start >= ZFS_MAX_DATASET_NAME_LEN) {
329 *why = NAME_ERR_TOOLONG;
334 } while (*end != '\0');
340 * For pool names, we have the same set of valid characters as described in
341 * dataset names, with the additional restriction that the pool name must begin
342 * with a letter. The pool names 'raidz' and 'mirror' are also reserved names
343 * that cannot be used.
345 * Returns 0 on success, -1 on error.
348 pool_namecheck(const char *pool, namecheck_err_t *why, char *what)
353 * Make sure the name is not too long.
354 * If we're creating a pool with version >= SPA_VERSION_DSL_SCRUB (v11)
355 * we need to account for additional space needed by the origin ds which
356 * will also be snapshotted: "poolname"+"/"+"$ORIGIN"+"@"+"$ORIGIN".
357 * Play it safe and enforce this limit even if the pool version is < 11
358 * so it can be upgraded without issues.
360 if (strlen(pool) >= (ZFS_MAX_DATASET_NAME_LEN - 2 -
361 strlen(ORIGIN_DIR_NAME) * 2)) {
363 *why = NAME_ERR_TOOLONG;
369 if (!valid_char(*c)) {
371 *why = NAME_ERR_INVALCHAR;
379 if (!(*pool >= 'a' && *pool <= 'z') &&
380 !(*pool >= 'A' && *pool <= 'Z')) {
382 *why = NAME_ERR_NOLETTER;
386 if (strcmp(pool, "mirror") == 0 || strcmp(pool, "raidz") == 0) {
388 *why = NAME_ERR_RESERVED;
392 if (pool[0] == 'c' && (pool[1] >= '0' && pool[1] <= '9')) {
394 *why = NAME_ERR_DISKLIKE;