|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | +/* |
| 3 | + * Copyright (C) 2026 Red Hat, Inc. |
| 4 | + */ |
| 5 | + |
| 6 | +/*\ |
| 7 | + * Check that futex(FUTEX_CMP_REQUEUE) returns EFAULT when uaddr or |
| 8 | + * uaddr2 points to unmapped memory, and EACCES (or EFAULT on older kernels) |
| 9 | + * when uaddr points to memory without read permission (PROT_NONE). |
| 10 | + * |
| 11 | + * The EACCES behavior for PROT_NONE was introduced in kernel 5.9. |
| 12 | + */ |
| 13 | + |
| 14 | +#include <errno.h> |
| 15 | +#include <sys/mman.h> |
| 16 | + |
| 17 | +#include "futextest.h" |
| 18 | + |
| 19 | +static futex_t futex_var = FUTEX_INITIALIZER; |
| 20 | +static futex_t *prot_none_addr; |
| 21 | + |
| 22 | +static struct futex_test_variants variants[] = { |
| 23 | +#if (__NR_futex != __LTP__NR_INVALID_SYSCALL) |
| 24 | + { .fntype = FUTEX_FN_FUTEX, .desc = "syscall with old kernel spec"}, |
| 25 | +#endif |
| 26 | + |
| 27 | +#if (__NR_futex_time64 != __LTP__NR_INVALID_SYSCALL) |
| 28 | + { .fntype = FUTEX_FN_FUTEX64, .desc = "syscall time64 with kernel spec"}, |
| 29 | +#endif |
| 30 | +}; |
| 31 | + |
| 32 | +static struct testcase { |
| 33 | + const char *desc; |
| 34 | + futex_t *uaddr; |
| 35 | + futex_t *uaddr2; |
| 36 | + int exp_errno; |
| 37 | +} testcases[3]; |
| 38 | + |
| 39 | +static void run(unsigned int n) |
| 40 | +{ |
| 41 | + struct futex_test_variants *tv = &variants[tst_variant]; |
| 42 | + struct testcase *tc = &testcases[n]; |
| 43 | + |
| 44 | + TST_EXP_FAIL(futex_cmp_requeue(tv->fntype, tc->uaddr, futex_var, |
| 45 | + tc->uaddr2, 1, 1, 0), tc->exp_errno, "%s", tc->desc); |
| 46 | +} |
| 47 | + |
| 48 | +static void setup(void) |
| 49 | +{ |
| 50 | + struct futex_test_variants *tv = &variants[tst_variant]; |
| 51 | + size_t pagesize = getpagesize(); |
| 52 | + futex_t *unmapped; |
| 53 | + |
| 54 | + tst_res(TINFO, "Testing variant: %s", tv->desc); |
| 55 | + futex_supported_by_kernel(tv->fntype); |
| 56 | + |
| 57 | + unmapped = SAFE_MMAP(NULL, pagesize, PROT_READ | PROT_WRITE, |
| 58 | + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 59 | + SAFE_MUNMAP(unmapped, pagesize); |
| 60 | + |
| 61 | + prot_none_addr = SAFE_MMAP(NULL, pagesize, PROT_NONE, |
| 62 | + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 63 | + |
| 64 | + testcases[0] = (struct testcase){ |
| 65 | + .desc = "uaddr unmapped", |
| 66 | + .uaddr = unmapped, |
| 67 | + .uaddr2 = &futex_var, |
| 68 | + .exp_errno = EFAULT, |
| 69 | + }; |
| 70 | + testcases[1] = (struct testcase){ |
| 71 | + .desc = "uaddr2 unmapped", |
| 72 | + .uaddr = &futex_var, |
| 73 | + .uaddr2 = unmapped, |
| 74 | + .exp_errno = EFAULT, |
| 75 | + }; |
| 76 | + testcases[2] = (struct testcase){ |
| 77 | + .desc = "uaddr PROT_NONE", |
| 78 | + .uaddr = prot_none_addr, |
| 79 | + .uaddr2 = &futex_var, |
| 80 | + .exp_errno = tst_kvercmp(5, 9, 0) >= 0 ? EACCES : EFAULT, |
| 81 | + }; |
| 82 | +} |
| 83 | + |
| 84 | +static void cleanup(void) |
| 85 | +{ |
| 86 | + if (prot_none_addr) |
| 87 | + SAFE_MUNMAP(prot_none_addr, getpagesize()); |
| 88 | +} |
| 89 | + |
| 90 | +static struct tst_test test = { |
| 91 | + .setup = setup, |
| 92 | + .cleanup = cleanup, |
| 93 | + .test = run, |
| 94 | + .tcnt = ARRAY_SIZE(testcases), |
| 95 | + .test_variants = ARRAY_SIZE(variants), |
| 96 | +}; |
0 commit comments