]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - zfs/module/zcommon/zfs_namecheck.c
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 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.
42 #include <sys/systm.h>
47 #include <sys/param.h>
48 #include <sys/nvpair.h>
49 #include "zfs_namecheck.h"
50 #include "zfs_deleg.h"
55 return ((c
>= 'a' && c
<= 'z') ||
56 (c
>= 'A' && c
<= 'Z') ||
57 (c
>= '0' && c
<= '9') ||
58 c
== '-' || c
== '_' || c
== '.' || c
== ':' || c
== ' ');
62 * Snapshot names must be made up of alphanumeric characters plus the following
68 zfs_component_namecheck(const char *path
, namecheck_err_t
*why
, char *what
)
72 if (strlen(path
) >= MAXNAMELEN
) {
74 *why
= NAME_ERR_TOOLONG
;
78 if (path
[0] == '\0') {
80 *why
= NAME_ERR_EMPTY_COMPONENT
;
84 for (loc
= path
; *loc
; loc
++) {
85 if (!valid_char(*loc
)) {
87 *why
= NAME_ERR_INVALCHAR
;
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.
103 permset_namecheck(const char *path
, namecheck_err_t
*why
, char *what
)
105 if (strlen(path
) >= ZFS_PERMSET_MAXLEN
) {
107 *why
= NAME_ERR_TOOLONG
;
111 if (path
[0] != '@') {
113 *why
= NAME_ERR_NO_AT
;
119 return (zfs_component_namecheck(&path
[1], why
, what
));
123 * Dataset names must be of the following form:
125 * [component][/]*[component][@component]
127 * Where each component is made up of alphanumeric characters plus the following
132 * We allow '%' here as we use that character internally to create unique
133 * names for temporary clones (for online recv).
136 dataset_namecheck(const char *path
, namecheck_err_t
*why
, char *what
)
138 const char *loc
, *end
;
142 * Make sure the name is not too long.
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.
149 * When HAVE_KOBJ_NAME_LEN is defined the maximum safe kobject name
150 * length is 20 bytes. This 20 bytes is broken down as follows to
151 * provide a maximum safe <pool>/<dataset>[@snapshot] length of only
152 * 18 bytes. To ensure bytes are left for <dataset>[@snapshot] the
153 * <pool> portition is futher limited to 9 bytes. For 2.6.27 and
154 * newer kernels this limit is set to MAXNAMELEN.
156 * <pool>/<dataset> + <partition> + <newline>
159 #ifdef HAVE_KOBJ_NAME_LEN
160 if (strlen(path
) > 18) {
162 if (strlen(path
) >= MAXNAMELEN
) {
163 #endif /* HAVE_KOBJ_NAME_LEN */
165 *why
= NAME_ERR_TOOLONG
;
169 /* Explicitly check for a leading slash. */
170 if (path
[0] == '/') {
172 *why
= NAME_ERR_LEADING_SLASH
;
176 if (path
[0] == '\0') {
178 *why
= NAME_ERR_EMPTY_COMPONENT
;
185 /* Find the end of this component */
187 while (*end
!= '/' && *end
!= '@' && *end
!= '\0')
190 if (*end
== '\0' && end
[-1] == '/') {
191 /* trailing slashes are not allowed */
193 *why
= NAME_ERR_TRAILING_SLASH
;
197 /* Zero-length components are not allowed */
201 * Make sure this is really a zero-length
202 * component and not a '@@'.
204 if (*end
== '@' && found_snapshot
) {
205 *why
= NAME_ERR_MULTIPLE_AT
;
207 *why
= NAME_ERR_EMPTY_COMPONENT
;
214 /* Validate the contents of this component */
216 if (!valid_char(*loc
) && *loc
!= '%') {
218 *why
= NAME_ERR_INVALCHAR
;
226 /* If we've reached the end of the string, we're OK */
232 * If we've found an @ symbol, indicate that we're in
233 * the snapshot component, and report a second '@'
234 * character as an error.
236 if (found_snapshot
) {
238 *why
= NAME_ERR_MULTIPLE_AT
;
246 * If there is a '/' in a snapshot name
247 * then report an error
249 if (*end
== '/' && found_snapshot
) {
251 *why
= NAME_ERR_TRAILING_SLASH
;
255 /* Update to the next component */
262 * mountpoint names must be of the following form:
264 * /[component][/]*[component][/]
267 mountpoint_namecheck(const char *path
, namecheck_err_t
*why
)
269 const char *start
, *end
;
272 * Make sure none of the mountpoint component names are too long.
273 * If a component name is too long then the mkdir of the mountpoint
274 * will fail but then the mountpoint property will be set to a value
275 * that can never be mounted. Better to fail before setting the prop.
276 * Extra slashes are OK, they will be tossed by the mountpoint mkdir.
279 if (path
== NULL
|| *path
!= '/') {
281 *why
= NAME_ERR_LEADING_SLASH
;
285 /* Skip leading slash */
289 while (*end
!= '/' && *end
!= '\0')
292 if (end
- start
>= MAXNAMELEN
) {
294 *why
= NAME_ERR_TOOLONG
;
299 } while (*end
!= '\0');
305 * For pool names, we have the same set of valid characters as described in
306 * dataset names, with the additional restriction that the pool name must begin
307 * with a letter. The pool names 'raidz' and 'mirror' are also reserved names
308 * that cannot be used.
311 pool_namecheck(const char *pool
, namecheck_err_t
*why
, char *what
)
316 * Make sure the name is not too long.
318 * ZPOOL_MAXNAMELEN is the maximum pool length used in the userland
319 * which is the same as MAXNAMELEN used in the kernel.
320 * If ZPOOL_MAXNAMELEN value is changed, make sure to cleanup all
321 * places using MAXNAMELEN.
323 * When HAVE_KOBJ_NAME_LEN is defined the maximum safe kobject name
324 * length is 20 bytes. This 20 bytes is broken down as follows to
325 * provide a maximum safe <pool>/<dataset>[@snapshot] length of only
326 * 18 bytes. To ensure bytes are left for <dataset>[@snapshot] the
327 * <pool> portition is futher limited to 8 bytes. For 2.6.27 and
328 * newer kernels this limit is set to MAXNAMELEN.
330 * <pool>/<dataset> + <partition> + <newline>
333 #ifdef HAVE_KOBJ_NAME_LEN
334 if (strlen(pool
) > 8) {
336 if (strlen(pool
) >= MAXNAMELEN
) {
337 #endif /* HAVE_KOBJ_NAME_LEN */
339 *why
= NAME_ERR_TOOLONG
;
345 if (!valid_char(*c
)) {
347 *why
= NAME_ERR_INVALCHAR
;
355 if (!(*pool
>= 'a' && *pool
<= 'z') &&
356 !(*pool
>= 'A' && *pool
<= 'Z')) {
358 *why
= NAME_ERR_NOLETTER
;
362 if (strcmp(pool
, "mirror") == 0 || strcmp(pool
, "raidz") == 0) {
364 *why
= NAME_ERR_RESERVED
;
368 if (pool
[0] == 'c' && (pool
[1] >= '0' && pool
[1] <= '9')) {
370 *why
= NAME_ERR_DISKLIKE
;
377 #if defined(_KERNEL) && defined(HAVE_SPL)
378 EXPORT_SYMBOL(pool_namecheck
);
379 EXPORT_SYMBOL(dataset_namecheck
);
380 EXPORT_SYMBOL(zfs_component_namecheck
);