DF-3198 / fix.diff
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | --- a/sys/sys/devfs_rules.h 2026-09-08 10:19:34.389993596 +0000 +++ b/sys/sys/devfs_rules.h 2026-09-08 10:20:01.509645297 +0000 @@ -50,13 +50,13 @@ u_long rule_cmd; char *mntpoint; - u_char mntpointlen; + u_int16_t mntpointlen; char *name; - u_char namlen; + u_int16_t namlen; char *linkname; - u_char linknamlen; + u_int16_t linknamlen; u_long dev_type; /* Type of device to which the rule applies */ --- a/sys/vfs/devfs/devfs_rules.c 2026-09-08 10:19:34.389993596 +0000 +++ b/sys/vfs/devfs/devfs_rules.c 2026-09-08 10:20:57.076931616 +0000 @@ -40,6 +40,7 @@ #include <sys/fcntl.h> #include <sys/device.h> #include <sys/mount.h> +#include <sys/jail.h> #include <sys/devfs.h> #include <sys/devfs_rules.h> @@ -57,6 +58,8 @@ static void devfs_rule_create_link(struct devfs_node *, struct devfs_rule *); static int devfs_rule_checkname(struct devfs_rule *, struct devfs_node *); +typedef TAILQ_HEAD(devfs_node_list, devfs_node) devfs_node_list_t; + static struct objcache *devfs_rule_cache; static struct lock devfs_rule_lock; @@ -79,7 +82,7 @@ devfs_rule_alloc(struct devfs_rule_ioctl *templ) { struct devfs_rule *rule; - size_t len; + size_t len, i, stars; rule = objcache_get(devfs_rule_cache, M_WAITOK); memset(rule, 0, sizeof(struct devfs_rule)); @@ -89,7 +92,7 @@ /* NOTREACHED */ len = strlen(templ->mntpoint); - if (len == 0) + if (len == 0 || len >= sizeof(templ->mntpoint)) goto error_out; /* NOTREACHED */ @@ -102,7 +105,22 @@ /* NOTREACHED */ len = strlen(templ->name); - if (len == 0) + if (len == 0 || len >= sizeof(templ->name)) + goto error_out; + /* NOTREACHED */ + + /* + * DF-3202: wildCaseCmp() recurses once per '*' in the pattern + * (80 byte frames); a few hundred stars exhaust the kernel + * stack during rule application. Real rules never come close + * to this many wildcards. + */ + stars = 0; + for (i = 0; i < len; ++i) { + if (templ->name[i] == '*') + ++stars; + } + if (stars > 32) goto error_out; /* NOTREACHED */ @@ -116,7 +134,7 @@ /* NOTREACHED */ len = strlen(templ->linkname); - if (len == 0) + if (len == 0 || len >= sizeof(templ->linkname)) goto error_out; /* NOTREACHED */ @@ -222,8 +240,19 @@ if ((node->node_type == Nlink) && (node->flags & DEVFS_RULE_CREATED)) { KKASSERT(node->link_target); node->flags &= ~DEVFS_RULE_CREATED; - --node->link_target->nlinks; - devfs_gc(node); + /* + * DF-3198: devfs_gc() frees arbitrary nodes (links pointing + * at us, empty dirs) while devfs_iterate_topology() may still + * hold them as cursors. Only unlink here; the caller frees + * the collected nodes after the walk completes. + * devfs_unlinkp() takes care of the nlinks decrement and of + * tearing down link_target. + */ + devfs_unlinkp(node); + if (unused != NULL) { + TAILQ_INSERT_TAIL((devfs_node_list_t *)unused, + node, link); + } } else if ((node->node_type == Ndev) && (node->d_dev)) { node->uid = node->d_dev->si_uid; node->gid = node->d_dev->si_gid; @@ -238,18 +267,34 @@ { size_t len = 0; char *path = NULL; - char *name, name_buf[PATH_MAX], buf[PATH_MAX]; + char *name; + /* + * DF-3201: these used to be PATH_MAX stack arrays; combined with + * the rule-application recursion they exhausted the kernel stack. + */ + char *name_buf = kmalloc(PATH_MAX, M_DEVFS, M_WAITOK); + char *buf = kmalloc(PATH_MAX, M_DEVFS, M_WAITOK); - if (rule->name[rule->namlen-1] == '*') { + if (rule->namlen > 0 && rule->name[rule->namlen-1] == '*') { devfs_resolve_name_path(rule->name, name_buf, &path, &name); len = strlen(name); --len; + /* + * DF-3199: the pattern is not necessarily a plain prefix of + * the node name; never strip more bytes than the node name + * holds (d_name is a separate exact-size kmalloc() buffer). + */ + if (len > node->d_dir.d_namlen) + len = node->d_dir.d_namlen; ksnprintf(buf, sizeof(buf), "%s%s", rule->linkname, node->d_dir.d_name+len); devfs_alias_create(buf, node, 1); } else { devfs_alias_create(rule->linkname, node, 1); } + + kfree(name_buf, M_DEVFS); + kfree(buf, M_DEVFS); } void * @@ -257,6 +302,7 @@ { struct devfs_rule *rule; struct mount *mp = node->mp; + static int rule_link_depth = 0; /* under devfs_lock + devfs_rule_lock */ int locked = 0; /* Check if it is locked already. if not, we acquire the devfs lock */ @@ -332,8 +378,16 @@ /* * This is a LINK rule, so we tell devfs to create * a link with the correct name to this node. + * + * DF-3201: a rule whose own output matches itself + * recurses without bound through allocp -> + * check_apply -> create_link; cap the depth. */ - devfs_rule_create_link(node, rule); + if (rule_link_depth < 8) { + ++rule_link_depth; + devfs_rule_create_link(node, rule); + --rule_link_depth; + } #if 0 devfs_alias_create(rule->linkname, node, 1); #endif @@ -361,19 +415,26 @@ devfs_rule_checkname(struct devfs_rule *rule, struct devfs_node *node) { struct devfs_node *parent = DEVFS_MNTDATA(node->mp)->root_node; + struct devfs_node *res; char *path = NULL; - char *name, name_buf[PATH_MAX]; + char *name; + char *name_buf = kmalloc(PATH_MAX, M_DEVFS, M_WAITOK); /* DF-3201 */ int no_match = 0; devfs_resolve_name_path(rule->name, name_buf, &path, &name); - parent = devfs_resolve_or_create_path(parent, path, 0); + res = devfs_resolve_or_create_path(parent, path, 0); - if (parent == NULL) + if (res == NULL) { + kfree(name_buf, M_DEVFS); return 0; /* no match */ + } + parent = res; /* Check if node is a child of the parent we found */ - if (node->parent != parent) + if (node->parent != parent) { + kfree(name_buf, M_DEVFS); return 0; /* no match */ + } #if 0 if (rule->rule_type & DEVFS_RULE_LINK) @@ -382,6 +443,7 @@ #endif no_match = devfs_WildCaseCmp(name, node->d_dir.d_name); + kfree(name_buf, M_DEVFS); return !no_match; } @@ -424,6 +486,16 @@ error = 0; rule = (struct devfs_rule_ioctl *)ap->a_data; + /* + * DF-3197: rule state is GLOBAL (devfs_rule_list) and APPLY/RESET + * walk every devfs mount on the system. Do not let jailed root + * rewrite host (or other jails') device visibility/permissions; the + * 0600 /dev/devfs node is not a jail boundary (jailed uid 0 passes + * the vnode permission check). + */ + if (jailed(curthread->td_ucred)) + return (EPERM); + switch(ap->a_cmd) { case DEVFS_RULE_ADD: error = devfs_rule_insert(rule); --- a/sys/vfs/devfs/devfs_core.c 2026-09-08 10:19:34.389993596 +0000 +++ b/sys/vfs/devfs/devfs_core.c 2026-09-08 10:21:51.856228023 +0000 @@ -1908,6 +1908,38 @@ * reset_rules_worker) on all mountpoints or only a specific * one. */ +/* + * DF-3198: two-pass rule reset. The topology walk only unlinks + * rule-created link nodes and collects them (see devfs_rule_reset_node); + * all freeing -- including nodes other collected links point at -- happens + * after the walk, so no devfs_iterate_topology() cursor can reference + * freed memory. Collected nodes already freed by an earlier links pass + * are recognized by DEVFS_DESTROYED (their memory stays stable: this runs + * on the devfs core thread and nothing on this path allocates from the + * devfs_node zone). + */ +static void +devfs_reset_mount(struct devfs_node *root) +{ + typedef TAILQ_HEAD(, devfs_node) devfs_dead_list_t; + devfs_dead_list_t dead = TAILQ_HEAD_INITIALIZER(dead); + struct devfs_node *n1, *n2; + + devfs_iterate_topology(root, devfs_rule_reset_node, &dead); + + TAILQ_FOREACH_MUTABLE(n1, &dead, link, n2) { + if (n1->flags & DEVFS_DESTROYED) + continue; + if (n1->nlinks > 0) + devfs_iterate_topology(root, + (devfs_iterate_callback_t *)devfs_gc_links_callback, + n1); + devfs_freep(n1); + } + devfs_iterate_topology(root, + (devfs_iterate_callback_t *)devfs_gc_dirs_callback, NULL); +} + static int devfs_apply_reset_rules_caller(char *mountto, int apply) { @@ -1915,16 +1947,22 @@ if (mountto[0] == '*') { TAILQ_FOREACH(mnt, &devfs_mnt_list, link) { - devfs_iterate_topology(mnt->root_node, - (apply)?(devfs_rule_check_apply):(devfs_rule_reset_node), - NULL); + if (apply) { + devfs_iterate_topology(mnt->root_node, + devfs_rule_check_apply, NULL); + } else { + devfs_reset_mount(mnt->root_node); + } } } else { TAILQ_FOREACH(mnt, &devfs_mnt_list, link) { if (!strcmp(mnt->mp->mnt_stat.f_mntonname, mountto)) { - devfs_iterate_topology(mnt->root_node, - (apply)?(devfs_rule_check_apply):(devfs_rule_reset_node), - NULL); + if (apply) { + devfs_iterate_topology(mnt->root_node, + devfs_rule_check_apply, NULL); + } else { + devfs_reset_mount(mnt->root_node); + } break; } } |