Deleted Added
full compact
zfs_namecheck.c (185029) zfs_namecheck.c (209962)
1/*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21/*
1/*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
23 * Use is subject to license terms.
24 */
25
26#pragma ident "%Z%%M% %I% %E% SMI"
27
28/*
29 * Common name validation routines for ZFS. These routines are shared by the
30 * userland code as well as the ioctl() layer to ensure that we don't
31 * inadvertently expose a hole through direct ioctl()s that never gets tested.
32 * In userland, however, we want significantly more information about _why_ the
33 * name is invalid. In the kernel, we only care whether it's valid or not.
34 * Each routine therefore takes a 'namecheck_err_t' which describes exactly why
35 * the name failed to validate.
36 *
37 * Each function returns 0 on success, -1 on error.
38 */
39
40#if defined(_KERNEL)
41#include <sys/systm.h>
42#else
43#include <string.h>
44#endif
45
46#include <sys/param.h>
47#include <sys/nvpair.h>
48#include "zfs_namecheck.h"
49#include "zfs_deleg.h"
50
51static int
52valid_char(char c)
53{
54 return ((c >= 'a' && c <= 'z') ||
55 (c >= 'A' && c <= 'Z') ||
56 (c >= '0' && c <= '9') ||
57 c == '-' || c == '_' || c == '.' || c == ':' || c == ' ');
58}
59
60/*
61 * Snapshot names must be made up of alphanumeric characters plus the following
62 * characters:
63 *
64 * [-_.:]
65 */
66int
67snapshot_namecheck(const char *path, namecheck_err_t *why, char *what)
68{
69 const char *loc;
70
71 if (strlen(path) >= MAXNAMELEN) {
72 if (why)
73 *why = NAME_ERR_TOOLONG;
74 return (-1);
75 }
76
77 if (path[0] == '\0') {
78 if (why)
79 *why = NAME_ERR_EMPTY_COMPONENT;
80 return (-1);
81 }
82
83 for (loc = path; *loc; loc++) {
84 if (!valid_char(*loc)) {
85 if (why) {
86 *why = NAME_ERR_INVALCHAR;
87 *what = *loc;
88 }
89 return (-1);
90 }
91 }
92 return (0);
93}
94
95
96/*
97 * Permissions set name must start with the letter '@' followed by the
98 * same character restrictions as snapshot names, except that the name
99 * cannot exceed 64 characters.
100 */
101int
102permset_namecheck(const char *path, namecheck_err_t *why, char *what)
103{
104 if (strlen(path) >= ZFS_PERMSET_MAXLEN) {
105 if (why)
106 *why = NAME_ERR_TOOLONG;
107 return (-1);
108 }
109
110 if (path[0] != '@') {
111 if (why) {
112 *why = NAME_ERR_NO_AT;
113 *what = path[0];
114 }
115 return (-1);
116 }
117
118 return (snapshot_namecheck(&path[1], why, what));
119}
120
121/*
122 * Dataset names must be of the following form:
123 *
124 * [component][/]*[component][@component]
125 *
126 * Where each component is made up of alphanumeric characters plus the following
127 * characters:
128 *
129 * [-_.:%]
130 *
131 * We allow '%' here as we use that character internally to create unique
132 * names for temporary clones (for online recv).
133 */
134int
135dataset_namecheck(const char *path, namecheck_err_t *why, char *what)
136{
137 const char *loc, *end;
138 int found_snapshot;
139
140 /*
141 * Make sure the name is not too long.
142 *
143 * ZFS_MAXNAMELEN is the maximum dataset length used in the userland
144 * which is the same as MAXNAMELEN used in the kernel.
145 * If ZFS_MAXNAMELEN value is changed, make sure to cleanup all
146 * places using MAXNAMELEN.
147 */
148
149 if (strlen(path) >= MAXNAMELEN) {
150 if (why)
151 *why = NAME_ERR_TOOLONG;
152 return (-1);
153 }
154
155 /* Explicitly check for a leading slash. */
156 if (path[0] == '/') {
157 if (why)
158 *why = NAME_ERR_LEADING_SLASH;
159 return (-1);
160 }
161
162 if (path[0] == '\0') {
163 if (why)
164 *why = NAME_ERR_EMPTY_COMPONENT;
165 return (-1);
166 }
167
168 loc = path;
169 found_snapshot = 0;
170 for (;;) {
171 /* Find the end of this component */
172 end = loc;
173 while (*end != '/' && *end != '@' && *end != '\0')
174 end++;
175
176 if (*end == '\0' && end[-1] == '/') {
177 /* trailing slashes are not allowed */
178 if (why)
179 *why = NAME_ERR_TRAILING_SLASH;
180 return (-1);
181 }
182
183 /* Zero-length components are not allowed */
184 if (loc == end) {
185 if (why) {
186 /*
187 * Make sure this is really a zero-length
188 * component and not a '@@'.
189 */
190 if (*end == '@' && found_snapshot) {
191 *why = NAME_ERR_MULTIPLE_AT;
192 } else {
193 *why = NAME_ERR_EMPTY_COMPONENT;
194 }
195 }
196
197 return (-1);
198 }
199
200 /* Validate the contents of this component */
201 while (loc != end) {
202 if (!valid_char(*loc) && *loc != '%') {
203 if (why) {
204 *why = NAME_ERR_INVALCHAR;
205 *what = *loc;
206 }
207 return (-1);
208 }
209 loc++;
210 }
211
212 /* If we've reached the end of the string, we're OK */
213 if (*end == '\0')
214 return (0);
215
216 if (*end == '@') {
217 /*
218 * If we've found an @ symbol, indicate that we're in
219 * the snapshot component, and report a second '@'
220 * character as an error.
221 */
222 if (found_snapshot) {
223 if (why)
224 *why = NAME_ERR_MULTIPLE_AT;
225 return (-1);
226 }
227
228 found_snapshot = 1;
229 }
230
231 /*
232 * If there is a '/' in a snapshot name
233 * then report an error
234 */
235 if (*end == '/' && found_snapshot) {
236 if (why)
237 *why = NAME_ERR_TRAILING_SLASH;
238 return (-1);
239 }
240
241 /* Update to the next component */
242 loc = end + 1;
243 }
244}
245
246
247/*
248 * mountpoint names must be of the following form:
249 *
250 * /[component][/]*[component][/]
251 */
252int
253mountpoint_namecheck(const char *path, namecheck_err_t *why)
254{
255 const char *start, *end;
256
257 /*
258 * Make sure none of the mountpoint component names are too long.
259 * If a component name is too long then the mkdir of the mountpoint
260 * will fail but then the mountpoint property will be set to a value
261 * that can never be mounted. Better to fail before setting the prop.
262 * Extra slashes are OK, they will be tossed by the mountpoint mkdir.
263 */
264
265 if (path == NULL || *path != '/') {
266 if (why)
267 *why = NAME_ERR_LEADING_SLASH;
268 return (-1);
269 }
270
271 /* Skip leading slash */
272 start = &path[1];
273 do {
274 end = start;
275 while (*end != '/' && *end != '\0')
276 end++;
277
278 if (end - start >= MAXNAMELEN) {
279 if (why)
280 *why = NAME_ERR_TOOLONG;
281 return (-1);
282 }
283 start = end + 1;
284
285 } while (*end != '\0');
286
287 return (0);
288}
289
290/*
291 * For pool names, we have the same set of valid characters as described in
292 * dataset names, with the additional restriction that the pool name must begin
293 * with a letter. The pool names 'raidz' and 'mirror' are also reserved names
294 * that cannot be used.
295 */
296int
297pool_namecheck(const char *pool, namecheck_err_t *why, char *what)
298{
299 const char *c;
300
301 /*
302 * Make sure the name is not too long.
303 *
304 * ZPOOL_MAXNAMELEN is the maximum pool length used in the userland
305 * which is the same as MAXNAMELEN used in the kernel.
306 * If ZPOOL_MAXNAMELEN value is changed, make sure to cleanup all
307 * places using MAXNAMELEN.
308 */
309 if (strlen(pool) >= MAXNAMELEN) {
310 if (why)
311 *why = NAME_ERR_TOOLONG;
312 return (-1);
313 }
314
315 c = pool;
316 while (*c != '\0') {
317 if (!valid_char(*c)) {
318 if (why) {
319 *why = NAME_ERR_INVALCHAR;
320 *what = *c;
321 }
322 return (-1);
323 }
324 c++;
325 }
326
327 if (!(*pool >= 'a' && *pool <= 'z') &&
328 !(*pool >= 'A' && *pool <= 'Z')) {
329 if (why)
330 *why = NAME_ERR_NOLETTER;
331 return (-1);
332 }
333
334 if (strcmp(pool, "mirror") == 0 || strcmp(pool, "raidz") == 0) {
335 if (why)
336 *why = NAME_ERR_RESERVED;
337 return (-1);
338 }
339
340 if (pool[0] == 'c' && (pool[1] >= '0' && pool[1] <= '9')) {
341 if (why)
342 *why = NAME_ERR_DISKLIKE;
343 return (-1);
344 }
345
346 return (0);
347}
26/*
27 * Common name validation routines for ZFS. These routines are shared by the
28 * userland code as well as the ioctl() layer to ensure that we don't
29 * inadvertently expose a hole through direct ioctl()s that never gets tested.
30 * In userland, however, we want significantly more information about _why_ the
31 * name is invalid. In the kernel, we only care whether it's valid or not.
32 * Each routine therefore takes a 'namecheck_err_t' which describes exactly why
33 * the name failed to validate.
34 *
35 * Each function returns 0 on success, -1 on error.
36 */
37
38#if defined(_KERNEL)
39#include <sys/systm.h>
40#else
41#include <string.h>
42#endif
43
44#include <sys/param.h>
45#include <sys/nvpair.h>
46#include "zfs_namecheck.h"
47#include "zfs_deleg.h"
48
49static int
50valid_char(char c)
51{
52 return ((c >= 'a' && c <= 'z') ||
53 (c >= 'A' && c <= 'Z') ||
54 (c >= '0' && c <= '9') ||
55 c == '-' || c == '_' || c == '.' || c == ':' || c == ' ');
56}
57
58/*
59 * Snapshot names must be made up of alphanumeric characters plus the following
60 * characters:
61 *
62 * [-_.:]
63 */
64int
65snapshot_namecheck(const char *path, namecheck_err_t *why, char *what)
66{
67 const char *loc;
68
69 if (strlen(path) >= MAXNAMELEN) {
70 if (why)
71 *why = NAME_ERR_TOOLONG;
72 return (-1);
73 }
74
75 if (path[0] == '\0') {
76 if (why)
77 *why = NAME_ERR_EMPTY_COMPONENT;
78 return (-1);
79 }
80
81 for (loc = path; *loc; loc++) {
82 if (!valid_char(*loc)) {
83 if (why) {
84 *why = NAME_ERR_INVALCHAR;
85 *what = *loc;
86 }
87 return (-1);
88 }
89 }
90 return (0);
91}
92
93
94/*
95 * Permissions set name must start with the letter '@' followed by the
96 * same character restrictions as snapshot names, except that the name
97 * cannot exceed 64 characters.
98 */
99int
100permset_namecheck(const char *path, namecheck_err_t *why, char *what)
101{
102 if (strlen(path) >= ZFS_PERMSET_MAXLEN) {
103 if (why)
104 *why = NAME_ERR_TOOLONG;
105 return (-1);
106 }
107
108 if (path[0] != '@') {
109 if (why) {
110 *why = NAME_ERR_NO_AT;
111 *what = path[0];
112 }
113 return (-1);
114 }
115
116 return (snapshot_namecheck(&path[1], why, what));
117}
118
119/*
120 * Dataset names must be of the following form:
121 *
122 * [component][/]*[component][@component]
123 *
124 * Where each component is made up of alphanumeric characters plus the following
125 * characters:
126 *
127 * [-_.:%]
128 *
129 * We allow '%' here as we use that character internally to create unique
130 * names for temporary clones (for online recv).
131 */
132int
133dataset_namecheck(const char *path, namecheck_err_t *why, char *what)
134{
135 const char *loc, *end;
136 int found_snapshot;
137
138 /*
139 * Make sure the name is not too long.
140 *
141 * ZFS_MAXNAMELEN is the maximum dataset length used in the userland
142 * which is the same as MAXNAMELEN used in the kernel.
143 * If ZFS_MAXNAMELEN value is changed, make sure to cleanup all
144 * places using MAXNAMELEN.
145 */
146
147 if (strlen(path) >= MAXNAMELEN) {
148 if (why)
149 *why = NAME_ERR_TOOLONG;
150 return (-1);
151 }
152
153 /* Explicitly check for a leading slash. */
154 if (path[0] == '/') {
155 if (why)
156 *why = NAME_ERR_LEADING_SLASH;
157 return (-1);
158 }
159
160 if (path[0] == '\0') {
161 if (why)
162 *why = NAME_ERR_EMPTY_COMPONENT;
163 return (-1);
164 }
165
166 loc = path;
167 found_snapshot = 0;
168 for (;;) {
169 /* Find the end of this component */
170 end = loc;
171 while (*end != '/' && *end != '@' && *end != '\0')
172 end++;
173
174 if (*end == '\0' && end[-1] == '/') {
175 /* trailing slashes are not allowed */
176 if (why)
177 *why = NAME_ERR_TRAILING_SLASH;
178 return (-1);
179 }
180
181 /* Zero-length components are not allowed */
182 if (loc == end) {
183 if (why) {
184 /*
185 * Make sure this is really a zero-length
186 * component and not a '@@'.
187 */
188 if (*end == '@' && found_snapshot) {
189 *why = NAME_ERR_MULTIPLE_AT;
190 } else {
191 *why = NAME_ERR_EMPTY_COMPONENT;
192 }
193 }
194
195 return (-1);
196 }
197
198 /* Validate the contents of this component */
199 while (loc != end) {
200 if (!valid_char(*loc) && *loc != '%') {
201 if (why) {
202 *why = NAME_ERR_INVALCHAR;
203 *what = *loc;
204 }
205 return (-1);
206 }
207 loc++;
208 }
209
210 /* If we've reached the end of the string, we're OK */
211 if (*end == '\0')
212 return (0);
213
214 if (*end == '@') {
215 /*
216 * If we've found an @ symbol, indicate that we're in
217 * the snapshot component, and report a second '@'
218 * character as an error.
219 */
220 if (found_snapshot) {
221 if (why)
222 *why = NAME_ERR_MULTIPLE_AT;
223 return (-1);
224 }
225
226 found_snapshot = 1;
227 }
228
229 /*
230 * If there is a '/' in a snapshot name
231 * then report an error
232 */
233 if (*end == '/' && found_snapshot) {
234 if (why)
235 *why = NAME_ERR_TRAILING_SLASH;
236 return (-1);
237 }
238
239 /* Update to the next component */
240 loc = end + 1;
241 }
242}
243
244
245/*
246 * mountpoint names must be of the following form:
247 *
248 * /[component][/]*[component][/]
249 */
250int
251mountpoint_namecheck(const char *path, namecheck_err_t *why)
252{
253 const char *start, *end;
254
255 /*
256 * Make sure none of the mountpoint component names are too long.
257 * If a component name is too long then the mkdir of the mountpoint
258 * will fail but then the mountpoint property will be set to a value
259 * that can never be mounted. Better to fail before setting the prop.
260 * Extra slashes are OK, they will be tossed by the mountpoint mkdir.
261 */
262
263 if (path == NULL || *path != '/') {
264 if (why)
265 *why = NAME_ERR_LEADING_SLASH;
266 return (-1);
267 }
268
269 /* Skip leading slash */
270 start = &path[1];
271 do {
272 end = start;
273 while (*end != '/' && *end != '\0')
274 end++;
275
276 if (end - start >= MAXNAMELEN) {
277 if (why)
278 *why = NAME_ERR_TOOLONG;
279 return (-1);
280 }
281 start = end + 1;
282
283 } while (*end != '\0');
284
285 return (0);
286}
287
288/*
289 * For pool names, we have the same set of valid characters as described in
290 * dataset names, with the additional restriction that the pool name must begin
291 * with a letter. The pool names 'raidz' and 'mirror' are also reserved names
292 * that cannot be used.
293 */
294int
295pool_namecheck(const char *pool, namecheck_err_t *why, char *what)
296{
297 const char *c;
298
299 /*
300 * Make sure the name is not too long.
301 *
302 * ZPOOL_MAXNAMELEN is the maximum pool length used in the userland
303 * which is the same as MAXNAMELEN used in the kernel.
304 * If ZPOOL_MAXNAMELEN value is changed, make sure to cleanup all
305 * places using MAXNAMELEN.
306 */
307 if (strlen(pool) >= MAXNAMELEN) {
308 if (why)
309 *why = NAME_ERR_TOOLONG;
310 return (-1);
311 }
312
313 c = pool;
314 while (*c != '\0') {
315 if (!valid_char(*c)) {
316 if (why) {
317 *why = NAME_ERR_INVALCHAR;
318 *what = *c;
319 }
320 return (-1);
321 }
322 c++;
323 }
324
325 if (!(*pool >= 'a' && *pool <= 'z') &&
326 !(*pool >= 'A' && *pool <= 'Z')) {
327 if (why)
328 *why = NAME_ERR_NOLETTER;
329 return (-1);
330 }
331
332 if (strcmp(pool, "mirror") == 0 || strcmp(pool, "raidz") == 0) {
333 if (why)
334 *why = NAME_ERR_RESERVED;
335 return (-1);
336 }
337
338 if (pool[0] == 'c' && (pool[1] >= '0' && pool[1] <= '9')) {
339 if (why)
340 *why = NAME_ERR_DISKLIKE;
341 return (-1);
342 }
343
344 return (0);
345}
348
349/*
350 * Check if the dataset name is private for internal usage.
351 * '$' is reserved for internal dataset names. e.g. "$MOS"
352 *
353 * Return 1 if the given name is used internally.
354 * Return 0 if it is not.
355 */
356int
357dataset_name_hidden(const char *name)
358{
359 if (strchr(name, '$') != NULL)
360 return (1);
361
362 return (0);
363}