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 seperator.
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
)
187 * Make sure the name is not too long.
189 if (strlen(path
) >= ZFS_MAX_DATASET_NAME_LEN
) {
191 *why
= NAME_ERR_TOOLONG
;
195 /* Explicitly check for a leading slash. */
196 if (path
[0] == '/') {
198 *why
= NAME_ERR_LEADING_SLASH
;
202 if (path
[0] == '\0') {
204 *why
= NAME_ERR_EMPTY_COMPONENT
;
208 const char *start
= path
;
209 boolean_t found_delim
= B_FALSE
;
211 /* Find the end of this component */
213 while (*end
!= '/' && *end
!= '@' && *end
!= '#' &&
217 if (*end
== '\0' && end
[-1] == '/') {
218 /* trailing slashes are not allowed */
220 *why
= NAME_ERR_TRAILING_SLASH
;
224 /* Validate the contents of this component */
225 for (const char *loc
= start
; loc
!= end
; loc
++) {
226 if (!valid_char(*loc
) && *loc
!= '%') {
228 *why
= NAME_ERR_INVALCHAR
;
235 /* Snapshot or bookmark delimiter found */
236 if (*end
== '@' || *end
== '#') {
237 /* Multiple delimiters are not allowed */
238 if (found_delim
!= 0) {
240 *why
= NAME_ERR_MULTIPLE_DELIMITERS
;
244 found_delim
= B_TRUE
;
247 /* Zero-length components are not allowed */
250 *why
= NAME_ERR_EMPTY_COMPONENT
;
254 /* If we've reached the end of the string, we're OK */
259 * If there is a '/' in a snapshot or bookmark name
260 * then report an error
262 if (*end
== '/' && found_delim
!= 0) {
264 *why
= NAME_ERR_TRAILING_SLASH
;
268 /* Update to the next component */
274 * Dataset is any entity, except bookmark
277 dataset_namecheck(const char *path
, namecheck_err_t
*why
, char *what
)
279 int ret
= entity_namecheck(path
, why
, what
);
281 if (ret
== 0 && strchr(path
, '#') != NULL
) {
283 *why
= NAME_ERR_INVALCHAR
;
293 * mountpoint names must be of the following form:
295 * /[component][/]*[component][/]
297 * Returns 0 on success, -1 on error.
300 mountpoint_namecheck(const char *path
, namecheck_err_t
*why
)
302 const char *start
, *end
;
305 * Make sure none of the mountpoint component names are too long.
306 * If a component name is too long then the mkdir of the mountpoint
307 * will fail but then the mountpoint property will be set to a value
308 * that can never be mounted. Better to fail before setting the prop.
309 * Extra slashes are OK, they will be tossed by the mountpoint mkdir.
312 if (path
== NULL
|| *path
!= '/') {
314 *why
= NAME_ERR_LEADING_SLASH
;
318 /* Skip leading slash */
322 while (*end
!= '/' && *end
!= '\0')
325 if (end
- start
>= ZFS_MAX_DATASET_NAME_LEN
) {
327 *why
= NAME_ERR_TOOLONG
;
332 } while (*end
!= '\0');
338 * For pool names, we have the same set of valid characters as described in
339 * dataset names, with the additional restriction that the pool name must begin
340 * with a letter. The pool names 'raidz' and 'mirror' are also reserved names
341 * that cannot be used.
343 * Returns 0 on success, -1 on error.
346 pool_namecheck(const char *pool
, namecheck_err_t
*why
, char *what
)
351 * Make sure the name is not too long.
352 * If we're creating a pool with version >= SPA_VERSION_DSL_SCRUB (v11)
353 * we need to account for additional space needed by the origin ds which
354 * will also be snapshotted: "poolname"+"/"+"$ORIGIN"+"@"+"$ORIGIN".
355 * Play it safe and enforce this limit even if the pool version is < 11
356 * so it can be upgraded without issues.
358 if (strlen(pool
) >= (ZFS_MAX_DATASET_NAME_LEN
- 2 -
359 strlen(ORIGIN_DIR_NAME
) * 2)) {
361 *why
= NAME_ERR_TOOLONG
;
367 if (!valid_char(*c
)) {
369 *why
= NAME_ERR_INVALCHAR
;
377 if (!(*pool
>= 'a' && *pool
<= 'z') &&
378 !(*pool
>= 'A' && *pool
<= 'Z')) {
380 *why
= NAME_ERR_NOLETTER
;
384 if (strcmp(pool
, "mirror") == 0 || strcmp(pool
, "raidz") == 0) {
386 *why
= NAME_ERR_RESERVED
;
390 if (pool
[0] == 'c' && (pool
[1] >= '0' && pool
[1] <= '9')) {
392 *why
= NAME_ERR_DISKLIKE
;
400 EXPORT_SYMBOL(pool_namecheck
);
401 EXPORT_SYMBOL(dataset_namecheck
);
402 EXPORT_SYMBOL(zfs_component_namecheck
);
403 EXPORT_SYMBOL(dataset_nestcheck
);
404 EXPORT_SYMBOL(get_dataset_depth
);
405 EXPORT_SYMBOL(zfs_max_dataset_nesting
);
407 module_param(zfs_max_dataset_nesting
, int, 0644);
408 MODULE_PARM_DESC(zfs_max_dataset_nesting
, "Maximum depth of nested datasets");