mirror of
https://sourceware.org/git/glibc.git
synced 2025-11-15 15:21:18 +03:00
It has been added on Linux 6.10 (8be7258aad44b5e25977a98db136f677fa6f4370) as a way to block operations such as mapping, moving to another location, shrinking the size, expanding the size, or modifying it to a pre-existing memory mapping. Although the system only works on 64-bit CPUs, the entrypoint was added for all ABIs (since the kernel might eventually implement it for additional ones and/or the ABI can execute on a 64-bit kernel). Checked on x86_64-linux-gnu and aarch64-linux-gnu. Reviewed-by: Collin Funk <collin.funk1@gmail.com>
68 lines
2.1 KiB
C
68 lines
2.1 KiB
C
/* Basic tests for mseal.
|
|
Copyright (C) 2025 Free Software Foundation, Inc.
|
|
This file is part of the GNU C Library.
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU Lesser General Public
|
|
License as published by the Free Software Foundation; either
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
License along with the GNU C Library; if not, see
|
|
<https://www.gnu.org/licenses/>. */
|
|
|
|
#include <errno.h>
|
|
#include <sys/mman.h>
|
|
#include <support/check.h>
|
|
#include <support/xunistd.h>
|
|
|
|
static int
|
|
do_test (void)
|
|
{
|
|
TEST_VERIFY (mseal (MAP_FAILED, 0, 0) == -1);
|
|
if (errno == ENOSYS || errno == EPERM)
|
|
FAIL_UNSUPPORTED ("kernel does not support mseal");
|
|
TEST_COMPARE (errno, EINVAL);
|
|
|
|
size_t pagesize = getpagesize ();
|
|
void *p = xmmap (NULL, 4 * pagesize, PROT_READ,
|
|
MAP_ANONYMOUS | MAP_PRIVATE, -1);
|
|
xmunmap (p + 2 * pagesize, pagesize);
|
|
|
|
/* Unaligned address. */
|
|
TEST_VERIFY_EXIT (mseal (p + 1, pagesize, 0) == -1);
|
|
TEST_COMPARE (errno, EINVAL);
|
|
|
|
/* Length too big. */
|
|
TEST_VERIFY_EXIT (mseal (p, 3 * pagesize, 0) == -1);
|
|
TEST_COMPARE (errno, ENOMEM);
|
|
|
|
TEST_VERIFY_EXIT (mseal (p, pagesize, 0) == 0);
|
|
/* Apply the same seal should be idempotent. */
|
|
TEST_VERIFY_EXIT (mseal (p, pagesize, 0) == 0);
|
|
|
|
TEST_VERIFY_EXIT (mprotect (p, pagesize, PROT_WRITE) == -1);
|
|
TEST_COMPARE (errno, EPERM);
|
|
|
|
TEST_VERIFY_EXIT (munmap (p, pagesize) == -1);
|
|
TEST_COMPARE (errno, EPERM);
|
|
|
|
TEST_VERIFY_EXIT (mremap (p, pagesize, 2 * pagesize, 0) == MAP_FAILED);
|
|
TEST_COMPARE (errno, EPERM);
|
|
|
|
TEST_VERIFY_EXIT (madvise (p, pagesize, MADV_DONTNEED) == -1);
|
|
TEST_COMPARE (errno, EPERM);
|
|
|
|
xmunmap (p + pagesize, pagesize);
|
|
xmunmap (p + 3 * pagesize, pagesize);
|
|
|
|
return 0;
|
|
}
|
|
|
|
#include <support/test-driver.c>
|