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