|
10 | 10 |
|
11 | 11 | #define MEMFD_NAME_MAX 249 |
12 | 12 |
|
13 | | -static int create_unlinked_temp(char *template_path, unsigned int flags) |
14 | | -{ |
15 | | - int fd; |
16 | | - if (flags & MFD_CLOEXEC) { |
17 | | - fd = mkostemp(template_path, O_CLOEXEC); |
18 | | - } else { |
19 | | - fd = mkstemp(template_path); |
20 | | - } |
21 | | - if (fd < 0) return -1; |
| 13 | +static int create_unlinked_temp(char *template_path, unsigned int flags) { |
| 14 | + int fd; |
| 15 | + if (flags & MFD_CLOEXEC) { |
| 16 | + fd = mkostemp(template_path, O_CLOEXEC); |
| 17 | + } else { |
| 18 | + fd = mkstemp(template_path); |
| 19 | + } |
| 20 | + if (fd < 0) |
| 21 | + return -1; |
22 | 22 |
|
23 | | - if (unlink(template_path) != 0) { |
24 | | - int saved_errno = errno; |
25 | | - close(fd); |
26 | | - errno = saved_errno; |
27 | | - return -1; |
28 | | - } |
| 23 | + if (unlink(template_path) != 0) { |
| 24 | + int saved_errno = errno; |
| 25 | + close(fd); |
| 26 | + errno = saved_errno; |
| 27 | + return -1; |
| 28 | + } |
29 | 29 |
|
30 | | - return fd; |
| 30 | + return fd; |
31 | 31 | } |
32 | 32 |
|
33 | 33 | // Returns the length of the name without the null character. |
34 | | -// If the returned length == dst_len, the src was too long. dst is not null terminated in this case. |
35 | | -static int sanitize_name(char *dst, size_t dst_len, const char *src) |
36 | | -{ |
37 | | - size_t i = 0; |
| 34 | +// If the returned length == dst_len, the src was too long. dst is not null |
| 35 | +// terminated in this case. |
| 36 | +static int sanitize_name(char *dst, size_t dst_len, const char *src) { |
| 37 | + size_t i = 0; |
38 | 38 |
|
39 | | - while (i < dst_len && src[i] != '\0') { |
40 | | - unsigned char c = (unsigned char)src[i]; |
41 | | - if (isalnum(c) || c == '_' || c == '-' || c == '.') { |
42 | | - dst[i] = (char)c; |
43 | | - } else { |
44 | | - dst[i] = '_'; |
45 | | - } |
46 | | - i++; |
47 | | - } |
| 39 | + while (i < dst_len && src[i] != '\0') { |
| 40 | + unsigned char c = (unsigned char)src[i]; |
| 41 | + if (isalnum(c) || c == '_' || c == '-' || c == '.') { |
| 42 | + dst[i] = (char)c; |
| 43 | + } else { |
| 44 | + dst[i] = '_'; |
| 45 | + } |
| 46 | + i++; |
| 47 | + } |
48 | 48 |
|
49 | | - return i; |
| 49 | + return i; |
50 | 50 | } |
51 | 51 |
|
| 52 | +int memfd_create(const char *name, unsigned int flags) { |
| 53 | + if (flags & ~(MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB)) { |
| 54 | + errno = EINVAL; |
| 55 | + return -1; |
| 56 | + } |
52 | 57 |
|
53 | | -int memfd_create(const char *name, unsigned int flags) |
54 | | -{ |
55 | | - if (flags & ~(MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB)) { |
56 | | - errno = EINVAL; |
57 | | - return -1; |
58 | | - } |
| 58 | + if (flags & (MFD_HUGETLB | MFD_ALLOW_SEALING)) { |
| 59 | + /* Seals are not currently supported by the WASIX fcntl subset. */ |
| 60 | + /* hugetlbfs is not currently supported by the WASIX filesystem subset. */ |
| 61 | + errno = ENOTSUP; |
| 62 | + return -1; |
| 63 | + } |
59 | 64 |
|
60 | | - if (flags & (MFD_HUGETLB | MFD_ALLOW_SEALING)) { |
61 | | - /* Seals are not currently supported by the WASIX fcntl subset. */ |
62 | | - /* hugetlbfs is not currently supported by the WASIX filesystem subset. */ |
63 | | - errno = ENOTSUP; |
64 | | - return -1; |
65 | | - } |
| 65 | + if (name == NULL) { |
| 66 | + errno = EFAULT; |
| 67 | + return -1; |
| 68 | + } |
66 | 69 |
|
67 | | - if (name == NULL) { |
68 | | - errno = EFAULT; |
69 | | - return -1; |
70 | | - } |
| 70 | + // Construct a string like "/tmp/.memfd-<name>-XXXXXX" |
| 71 | + char tmp_template[sizeof("/tmp/.memfd-") - 1 + MEMFD_NAME_MAX + |
| 72 | + sizeof("-XXXXXX") - 1 + 1]; |
| 73 | + memcpy(tmp_template, "/tmp/.memfd-", sizeof("/tmp/.memfd-") - 1); |
| 74 | + int name_len = sanitize_name(tmp_template + sizeof("/tmp/.memfd-") - 1, |
| 75 | + MEMFD_NAME_MAX + 1, name); |
| 76 | + if (name_len > MEMFD_NAME_MAX) { |
| 77 | + errno = EINVAL; |
| 78 | + return -1; |
| 79 | + } |
| 80 | + memcpy(tmp_template + sizeof("/tmp/.memfd-") - 1 + name_len, "-XXXXXX", |
| 81 | + sizeof("-XXXXXX")); |
71 | 82 |
|
72 | | - // Construct a string like "/tmp/.memfd-<name>-XXXXXX" |
73 | | - char tmp_template[sizeof("/tmp/.memfd-")-1 + MEMFD_NAME_MAX + sizeof("-XXXXXX")-1 + 1]; |
74 | | - memcpy(tmp_template, "/tmp/.memfd-", sizeof("/tmp/.memfd-") -1); |
75 | | - int name_len = sanitize_name(tmp_template + sizeof("/tmp/.memfd-") - 1, MEMFD_NAME_MAX + 1, name); |
76 | | - if (name_len > MEMFD_NAME_MAX) { |
77 | | - errno = EINVAL; |
78 | | - return -1; |
79 | | - } |
80 | | - memcpy(tmp_template + sizeof("/tmp/.memfd-") - 1 + name_len, "-XXXXXX", sizeof("-XXXXXX")); |
| 83 | + return create_unlinked_temp(tmp_template, flags); |
| 84 | + if (fd >= 0) |
| 85 | + return fd; |
81 | 86 |
|
82 | | - return create_unlinked_temp(tmp_template, flags); |
83 | | - if (fd >= 0) return fd; |
84 | | - |
85 | | - // If that failed, try ".memfd-<name>-XXXXXX" in the current working directory. |
86 | | - // Restore the -XXXXXX placeholder because it got overwritten by the previous call to mkstemp. |
87 | | - memcpy(tmp_template + sizeof("/tmp/.memfd-") - 1 + name_len, "-XXXXXX", sizeof("-XXXXXX")); |
88 | | - return create_unlinked_temp(tmp_template + sizeof("/tmp/") - 1, flags); |
| 87 | + // If that failed, try ".memfd-<name>-XXXXXX" in the current working |
| 88 | + // directory. Restore the -XXXXXX placeholder because it got overwritten by |
| 89 | + // the previous call to mkstemp. |
| 90 | + memcpy(tmp_template + sizeof("/tmp/.memfd-") - 1 + name_len, "-XXXXXX", |
| 91 | + sizeof("-XXXXXX")); |
| 92 | + return create_unlinked_temp(tmp_template + sizeof("/tmp/") - 1, flags); |
89 | 93 | } |
0 commit comments