| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
|---|
| 3 | | - * Licensed under the GPL |
|---|
| 4 | 4 | */ |
|---|
| 5 | 5 | |
|---|
| 6 | 6 | #include <stdio.h> |
|---|
| .. | .. |
|---|
| 97 | 97 | while ((ent = readdir(directory)) != NULL) { |
|---|
| 98 | 98 | if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) |
|---|
| 99 | 99 | continue; |
|---|
| 100 | | - len = strlen(dir) + sizeof("/") + strlen(ent->d_name) + 1; |
|---|
| 100 | + len = strlen(dir) + strlen("/") + strlen(ent->d_name) + 1; |
|---|
| 101 | 101 | if (len > sizeof(file)) { |
|---|
| 102 | 102 | ret = -E2BIG; |
|---|
| 103 | 103 | goto out; |
|---|
| .. | .. |
|---|
| 135 | 135 | */ |
|---|
| 136 | 136 | static inline int is_umdir_used(char *dir) |
|---|
| 137 | 137 | { |
|---|
| 138 | | - char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")]; |
|---|
| 139 | | - char pid[sizeof("nnnnn\0")], *end; |
|---|
| 138 | + char pid[sizeof("nnnnnnnnn")], *end, *file; |
|---|
| 140 | 139 | int dead, fd, p, n, err; |
|---|
| 140 | + size_t filelen = strlen(dir) + sizeof("/pid") + 1; |
|---|
| 141 | 141 | |
|---|
| 142 | | - n = snprintf(file, sizeof(file), "%s/pid", dir); |
|---|
| 143 | | - if (n >= sizeof(file)) { |
|---|
| 144 | | - printk(UM_KERN_ERR "is_umdir_used - pid filename too long\n"); |
|---|
| 145 | | - err = -E2BIG; |
|---|
| 146 | | - goto out; |
|---|
| 147 | | - } |
|---|
| 142 | + file = malloc(filelen); |
|---|
| 143 | + if (!file) |
|---|
| 144 | + return -ENOMEM; |
|---|
| 145 | + |
|---|
| 146 | + snprintf(file, filelen, "%s/pid", dir); |
|---|
| 148 | 147 | |
|---|
| 149 | 148 | dead = 0; |
|---|
| 150 | 149 | fd = open(file, O_RDONLY); |
|---|
| .. | .. |
|---|
| 185 | 184 | out_close: |
|---|
| 186 | 185 | close(fd); |
|---|
| 187 | 186 | out: |
|---|
| 187 | + free(file); |
|---|
| 188 | 188 | return 0; |
|---|
| 189 | 189 | } |
|---|
| 190 | 190 | |
|---|
| .. | .. |
|---|
| 210 | 210 | |
|---|
| 211 | 211 | static void __init create_pid_file(void) |
|---|
| 212 | 212 | { |
|---|
| 213 | | - char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")]; |
|---|
| 214 | | - char pid[sizeof("nnnnn\0")]; |
|---|
| 213 | + char pid[sizeof("nnnnnnnnn")], *file; |
|---|
| 215 | 214 | int fd, n; |
|---|
| 216 | 215 | |
|---|
| 217 | | - if (umid_file_name("pid", file, sizeof(file))) |
|---|
| 216 | + n = strlen(uml_dir) + UMID_LEN + sizeof("/pid"); |
|---|
| 217 | + file = malloc(n); |
|---|
| 218 | + if (!file) |
|---|
| 218 | 219 | return; |
|---|
| 220 | + |
|---|
| 221 | + if (umid_file_name("pid", file, n)) |
|---|
| 222 | + goto out; |
|---|
| 219 | 223 | |
|---|
| 220 | 224 | fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0644); |
|---|
| 221 | 225 | if (fd < 0) { |
|---|
| 222 | 226 | printk(UM_KERN_ERR "Open of machine pid file \"%s\" failed: " |
|---|
| 223 | 227 | "%s\n", file, strerror(errno)); |
|---|
| 224 | | - return; |
|---|
| 228 | + goto out; |
|---|
| 225 | 229 | } |
|---|
| 226 | 230 | |
|---|
| 227 | 231 | snprintf(pid, sizeof(pid), "%d\n", getpid()); |
|---|
| .. | .. |
|---|
| 231 | 235 | errno); |
|---|
| 232 | 236 | |
|---|
| 233 | 237 | close(fd); |
|---|
| 238 | +out: |
|---|
| 239 | + free(file); |
|---|
| 234 | 240 | } |
|---|
| 235 | 241 | |
|---|
| 236 | 242 | int __init set_umid(char *name) |
|---|
| .. | .. |
|---|
| 385 | 391 | |
|---|
| 386 | 392 | static void remove_umid_dir(void) |
|---|
| 387 | 393 | { |
|---|
| 388 | | - char dir[strlen(uml_dir) + UMID_LEN + 1], err; |
|---|
| 394 | + char *dir, err; |
|---|
| 395 | + |
|---|
| 396 | + dir = malloc(strlen(uml_dir) + UMID_LEN + 1); |
|---|
| 397 | + if (!dir) |
|---|
| 398 | + return; |
|---|
| 389 | 399 | |
|---|
| 390 | 400 | sprintf(dir, "%s%s", uml_dir, umid); |
|---|
| 391 | 401 | err = remove_files_and_dir(dir); |
|---|
| 392 | 402 | if (err) |
|---|
| 393 | 403 | os_warn("%s - remove_files_and_dir failed with err = %d\n", |
|---|
| 394 | 404 | __func__, err); |
|---|
| 405 | + |
|---|
| 406 | + free(dir); |
|---|
| 395 | 407 | } |
|---|
| 396 | 408 | |
|---|
| 397 | 409 | __uml_exitcall(remove_umid_dir); |
|---|