casaroli opened a new pull request, #21647: URL: https://github.com/apache/nuttx/pull/21647
## Summary memfd_create() opens its file with `O_RDWR | O_EXCL | flags` and no `O_CREAT`, so it fails with ENOENT for `flags` 0, `MFD_CLOEXEC` and `MFD_ALLOW_SEALING`. It works only when the caller puts `O_CREAT` in `flags`, which is not a memfd_create() flag (apps testing/fs/memfd does this). Two more problems. memfd_create() gives all of `flags` to open(), and `MFD_ALLOW_SEALING` (`1 << 16`) has the bit of `O_DIRECTORY`. A name longer than `NAME_MAX` minus 6 (for shmfs, also minus the directory) makes memfd_create() fail with ENAMETOOLONG: with the default `NAME_MAX` of 32, a shmfs memfd_create() fails for a name of more than 20 characters. The name is only for debug, and Linux accepts names of up to 249 characters. This PR adds `O_CREAT`, gives only `MFD_CLOEXEC` to open(), and cuts the name so that the file name fits in `NAME_MAX`. ## Impact libc only, with `CONFIG_LIBC_MEMFD_SHMFS` or `CONFIG_LIBC_MEMFD_TMPFS`. The same in flat, protected and kernel builds; no arch code; no Kconfig or build file change. What a program sees: - memfd_create() with `flags` 0, `MFD_CLOEXEC` or `MFD_ALLOW_SEALING` now gives a descriptor (before: -1 with ENOENT). - A long name now gives a descriptor (before: -1 with ENAMETOOLONG). The file gets the first characters of the name. - A caller that gives `O_CREAT | O_CLOEXEC` in `flags` works as before. - Bits of `flags` other than `MFD_CLOEXEC` no longer go to open(). A caller that gave other open() flags (for example `O_NONBLOCK`) in `flags` no longer gets them on the descriptor. memfd_create() ignores these bits and does not give EINVAL as Linux does: testing/fs/memfd and other callers give `O_CREAT` in `flags`, and EINVAL would break them. Size: lib_memfd.o text +10 bytes (shmfs) and +14 bytes (tmpfs) on qemu-armv7a:nsh with arm-none-eabi-gcc 15.3. Configurations without memfd do not build the file. No Documentation page describes memfd_create(). ## Testing Test program `memfdcreate.c`: flags 0, `MFD_CLOEXEC` (checks `FD_CLOEXEC`), `MFD_ALLOW_SEALING`, a name of 200 characters, ftruncate()/fstat(), write/read, a `MAP_SHARED` mmap(), two files with the same name, and `O_CREAT | O_CLOEXEC` as flags. The new apps test testing/libc/memfd_create (apache/nuttx-apps#NNNN) checks the same cases without mmap() and `O_CREAT`. | Configuration | master | this PR | |---|---|---| | qemu-armv8a:knsh (kernel build, arm64, QEMU, `LIBC_MEMFD_SHMFS`) | memfdcreate: only the `O_CREAT` case passes (ENOENT; ENAMETOOLONG for the long name); apps test 4 errors | memfdcreate 9/9; apps test 0 errors | | qemu-armv8a:nsh (flat build, arm64, QEMU, `LIBC_MEMFD_TMPFS`, `MM_KASAN`) | memfdcreate: only the `O_CREAT` case passes; apps test 4 errors | memfdcreate 9/9; apps test 0 errors | | mps2-an500:knsh (protected build, Cortex-M7, QEMU, `LIBC_MEMFD_TMPFS`) | memfdcreate without mmap(): only the `O_CREAT` case passes; apps test 4 errors | memfdcreate without mmap() 8/8, apps test 0 errors | - ostest exits 0 on qemu-armv8a:knsh, qemu-armv8a:nsh (default) and qemu-armv8a:nsh (`LIBC_MEMFD_TMPFS`, `MM_KASAN`), with master and with this PR. - apps testing/fs/memfd (`memfd_test`, 50 threads with `O_CREAT | O_CLOEXEC`) on the flat `MM_KASAN` build: 50/50 threads pass with master and with this PR, no KASAN report. - qemu-armv7a:nsh with `LIBC_MEMFD_SHMFS` and with `LIBC_MEMFD_TMPFS`, `EXTRAFLAGS=-Werror` (32-bit, arm-none-eabi-gcc 15.3): builds with no warning. - CMake: qemu-armv8a:nsh with `LIBC_MEMFD_TMPFS` builds. - With this PR, the mmap() case on mps2-an500:knsh gives an MPU fault in the user task: tmpfs_mmap() gives the address of the file data in the kernel heap. That is a tmpfs issue in protected builds and not part of this PR. `tools/checkpatch.sh -c -u -m -g origin/master..HEAD` passes. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
