Commit Graph

437 Commits

Author SHA1 Message Date
fangxinyong 2d73e86b47 binfmt: support euid of process set from the file system
From posix spec, if set-user-ID bit is set in the file permissions,
then the effective user ID of the new process shall be set to the
user ID of the new process image file.
Let's ignore whether ST_NOSUID is set on the mounted file system.

https://pubs.opengroup.org/onlinepubs/007904875/functions/exec.html

test step:
hello example build as a module and call geteuid and getegid API.
then set file binary set-user-ID bit on the host.

$ chmod +s apps/bin/hello

nsh> mount -t hostfs -o fs=. /data
nsh> ls -l /data/apps/bin/hello
 -rwsrwsr-x    1000    1000    9264 /data/apps/bin/hello
nsh> /data/apps/bin/hello
geteuid:1000
getegid:1000

Signed-off-by: fangxinyong <fangxinyong@xiaomi.com>
2023-08-12 02:18:25 +08:00
hujun5 b185f8d889 binfmt: add enter_critical_section
adding enter_critical_section to ensure non preemption in smp
Signed-off-by: hujun5 <hujun5@xiaomi.com>
2023-08-10 20:46:59 +08:00
Xiang Xiao 6b4e5c0d15 binfmt: Change the default of BINFMT_DISABLE to DEFAULT_SMALL
to optimize the image size when and DEFAULT_SMALL is enabled
and refresh the defconfig in boards/

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-08-10 11:35:41 +03:00
hujun5 9f1cb4135b binfmt/elf: bss section should init to zero
Signed-off-by: hujun5 <hujun5@xiaomi.com>
2023-07-29 06:54:25 -07:00
wanggang26 abc1cade35 binfmt/elf: Support to load ET_EXEC in flat mode
Signed-off-by: wanggang26 <wanggang26@xiaomi.com>
2023-07-28 08:35:27 -07:00
wanggang26 9d67c7b6ac binfmt/elf: both regular file and non-regular file (such as /dev/node) should be accessible
Signed-off-by: wanggang26 <wanggang26@xiaomi.com>
2023-07-28 07:52:37 -07:00
hujun5 4109908013 binfmt: remove sched_[un]lock
In the current usage mode, multiple tasks will not modify g_binfmts simultaneously

Signed-off-by: hujun5 <hujun5@xiaomi.com>
2023-07-26 09:43:51 -07:00
Xiang Xiao f3269a6caa sched: Rename DEBUG_TCBINFO to ARCH_HAVE_TCBINFO
and select if the arch support to define g_tcbinfo variable

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-07-26 09:33:38 +02:00
Xiang Xiao 879dab08a6 binfmt: Move elf_allocbuffer to elf_sectname and elf_symname
it's better to allocate the buffer just before really use it.

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-07-16 14:17:07 +03:00
Xiang Xiao ebcb03dce9 libc/symtab: Don't include symtab.h in the header files
to unify the inclusion of symtab.h only from the source files

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-07-10 23:03:17 +03:00
Xiang Xiao 229293f9f8 binfmt: Move [elf|nxflat]_[un]initialize to private header file
like what builtin binary format do

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-07-10 23:03:17 +03:00
chao an 6ee9ec7656 build: add initial cmake build system
1. Update all CMakeLists.txt to adapt to new layout
2. Fix cmake build break
3. Update all new file license
4. Fully compatible with current compilation environment(use configure.sh or cmake as you choose)

------------------

How to test

From within nuttx/. Configure:

cmake -B build -DBOARD_CONFIG=sim/nsh -GNinja
cmake -B build -DBOARD_CONFIG=sim:nsh -GNinja
cmake -B build -DBOARD_CONFIG=sabre-6quad/smp -GNinja
cmake -B build -DBOARD_CONFIG=lm3s6965-ek/qemu-flat -GNinja

(or full path in custom board) :
cmake -B build -DBOARD_CONFIG=$PWD/boards/sim/sim/sim/configs/nsh -GNinja

This uses ninja generator (install with sudo apt install ninja-build). To build:

$ cmake --build build

menuconfig:

$ cmake --build build -t menuconfig

--------------------------

2. cmake/build: reformat the cmake style by cmake-format

https://github.com/cheshirekow/cmake_format

$ pip install cmakelang

$ for i in `find -name CMakeLists.txt`;do cmake-format $i -o $i;done
$ for i in `find -name *\.cmake`;do cmake-format $i -o $i;done

Co-authored-by: Matias N <matias@protobits.dev>
Signed-off-by: chao an <anchao@xiaomi.com>
2023-07-08 13:50:48 +08:00
Xiang Xiao 8f38fa6062 symtab: Remove the unnessary inclusion of nuttx/symtab.h
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-07-06 09:24:02 -03:00
Xiang Xiao 1b2f37259a binfmt/elf: Replace nx_stat with file_stat
since kernel code prefer to use file_ API

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-07-06 09:20:24 -03:00
Xiang Xiao 7bb97f7e22 elf: Replace {0x7f, 'E', 'L', 'F'} to EI_MAGIC
to avoid the duplication of the magic number.

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-07-06 09:15:50 -03:00
wanggang26 57d521cc59 binfmt: Immediately exit from elf_loadbinary if elf format isn't supported 2023-07-03 00:52:16 +08:00
Stuart Ianna 6492f0172e binfmt/elf: Allow the userspace ELF type to be defined by board configuration.
This change allows boards to define an additional kconfig option, which specifies the final link format of application executables.

By selecting `CONFIG_BINFMT_ELF_RELOCATABLE`, and providing an appropriate linker script, applications can be fully linked, removing the need to process relocations.
2023-06-28 15:16:28 +08:00
Xiang Xiao a8e0a5faa4 sched: Remove the unnecessary cast from pid_t to int
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-06-19 11:37:34 +03:00
Petro Karashchenko 1b801a5bbc style: remove extra spaces and align parameters
Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
2023-06-11 12:55:29 +08:00
Ville Juven a636edcbe4 addrenv/kstack: Allocate the kernel stack before initializing tcb
This is preparation to use kernel stack for everything when the user
process enters the kernel. Now the user stack is in use when the user
process runs a system call, which might not be the safest option.
2023-06-09 13:53:27 +08:00
chao an 090a52c5fb elf/coredump: alignment stack buffer to 64 to match gdb request
Signed-off-by: chao an <anchao@xiaomi.com>
2023-05-30 23:00:23 +08:00
Stuart Ianna a05e8fd9ff binfmt/elf: Support loading fully linked executables.
The following changes make it possible for Nuttx to load binaries in ELF format which are fully linked.

The change does not include the necessary modifications to produce such binaries. In order to build an applicable binary:
 - The userspace applications linker script (`gnu-elf.ld`) needs to modified so the data and text section origin's match those setup by the address environment.
 - The makefile used, in `apps/import/Make.defs` needs to remove the `-r` LDELFFLAG.
2023-05-26 10:37:45 -03:00
chao an 78006f9824 elf/coredump: add sanity checks for stack pointer
stack pointer may be invalid value if in SMP mode, add sanity checks
to avoid invalid access

Signed-off-by: chao an <anchao@xiaomi.com>
2023-05-25 11:37:30 +08:00
chao an 24f4216066 elf/coredump: add support of dump task stack without memory segments
Signed-off-by: chao an <anchao@xiaomi.com>
2023-05-24 22:34:47 +08:00
Xiang Xiao 7990f90915 Indent the define statement by two spaces
follow the code style convention

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2023-05-21 09:52:08 -03:00
chao an 064c75aa92 elf/coredump: correct register offset after xcp.regs update
The offset should be calculated from pointer xcp.regs

Signed-off-by: chao an <anchao@xiaomi.com>
2023-05-18 20:13:05 +08:00
chao an 507c8145a9 sched/spawn: remove spawn proxy thread to simplify task/posix_spawn()
The spawn proxy thread is a special existence in NuttX, usually some developers
spend a lot of time on stack overflow of spawn proxy thread:

https://github.com/apache/nuttx/issues/9046
https://github.com/apache/nuttx/pull/9081

In order to avoid similar issues, this PR will remove spawn proxy thread to simplify
the process of task/posix_spawn().

1. Postpone the related processing of spawn file actions until after task_init()
2. Delete the temporary thread of spawn proxy and related global variables

Signed-off-by: chao an <anchao@xiaomi.com>
2023-04-27 17:35:58 +08:00
chao an e3ca288087 binfmt/execmodule: correct destroy flow
1. stack_alloc_ptr should be freed in kernel mode
2. replace nxsched_release_tcb() to nxtask_uninit() to remove tcb from inactive list

Signed-off-by: chao an <anchao@xiaomi.com>
2023-04-27 00:39:38 +08:00
Ville Juven d48114a4b7 sched/addrenv.c: Implement re-entrancy for addrenv_select()
Store the old environment in a local context so another temporary address
environment can be selected. This can happen especially when a process
is being loaded (the new process's mappings are temporarily instantiated)
and and interrupt occurs.
2023-04-25 14:33:19 +02:00
Stuart Ianna f4dcdbec0e libelf: Support prioritized init and fini arrays.
Add a wildcard operator to gnu-elf.ld to ensure all c++ constructors
and destruction are included in partially linked application binaries.
2023-04-22 01:28:01 +08:00
Ville Juven 64d8249895 sched/addrenv, binfmt: Always allocate address environment from heap
Instead of using a volatile storage for the address environment in the
binfmt / loadinfo structures, always allocate the address environment
from kheap.

This serves two purposes:
- If the task creation fails, any kernel thread that depends on the
  address environment created during task creation will not lose their
  mappings (because they hold a reference to it)
- The current address environment variable (g_addrenv) will NEVER contain
  a stale / incorrect value
- Releasing the address environment is simplified as any pointer given
  to addrenv_drop() can be assumed to be heap memory
- Makes the kludge function addrenv_clear_current irrelevant, as the
  system will NEVER have invalid mappings any more
2023-04-22 00:43:30 +08:00
Ville Juven b982c1747b sched/addrenv: Miscellaneous clean-up and fixes
- Remove the temporary "saved" variable when temporarily changing MMU
  mappings to access another process's memory. The fact that it has an
  address environment is enough to make the choice
- Restore nxflat_addrenv_restore-macro. It was accidentally lost when
  the address environment handling was re-factored.
2023-04-15 13:21:48 +09:00
chao an 3a0fdb019d nuttx: replace all ARRAY_SIZE()/ARRAYSIZE() to nitems()
Signed-off-by: chao an <anchao@xiaomi.com>
2023-02-09 20:05:44 +08:00
Ville Juven f4b82b6405 sched/addrenv: Remove up_addrenv_restore
The function is not relevant any longer, remove it. Also remove
save_addrenv_t, the parameter taken by up_addrenv_restore.

Implement addrenv_select() / addrenv_restore() to handle the temporary
instantiation of address environments, e.g. when a process is being
created.
2023-02-08 02:51:23 +08:00
Ville Juven 5713d85df0 group/group_addrenv: Move address environment from group -> tcb
Detach the address environment handling from the group structure to the
tcb. This is preparation to fix rare cases where the system (MMU) is left
without a valid page directory, e.g. when a process exits.
2023-02-08 02:51:23 +08:00
Gustavo Henrique Nihei e6b204f438 nuttx: Use MIN/MAX definitions from "sys/param.h"
Signed-off-by: Gustavo Henrique Nihei <gustavo.nihei@espressif.com>
2023-02-01 23:47:44 +08:00
Ville Juven fb12b6e3a9 libelf/elf_addrenv_free(): Don't instantiate address environment prior to destroying it
Same as with group_free(), there is no need to instantiate the address
environment to destroy it.

The only problem was the ARM implementation modified the L1 mappings
in up_addrenv_destroy(), which it no longer does.
2023-01-18 11:02:19 +08:00
Jukka Laitinen a2a10c87e3 mm/shm: Switch to use process' common virtual memory region allocator
- Also remove the nuttx private shm.h file nuttx/mm/shm.h, which became redundant
- Also remove the gran allocator initialization/release in binfmt since common
  vpage allocator is initialized in group_create/group_leave

Signed-off-by: Jukka Laitinen <jukkax@ssrc.tii.ae>
2023-01-13 02:20:13 +08:00
Jukka Laitinen 70de321de3 arch/Kconfig: remove virtual memory allocator dependency from MM_SHM
The dependency should be vice versa; the MM_SHM should depend on the
existence of the virtual memory range allocator.

Create a new CONFIG flag CONFIG_ARCH_VMA_MAPPING, which will define that
there is a virtual memory range allocator. Make MM_SHM select that flag

Signed-off-by: Jukka Laitinen <jukkax@ssrc.tii.ae>
2023-01-13 02:20:13 +08:00
Petro Karashchenko b107e4f417 nuttx: unify MIN, MAX and ABS macro definition across the code
Signed-off-by: Petro Karashchenko <petro.karashchenko@gmail.com>
2022-12-21 09:31:28 +08:00
yinshengkai 377bf97613 stream: replace the stream function pointer with a macro
Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
2022-11-25 21:41:51 +08:00
yinshengkai 85f727f232 tools: replace INCDIR to Makefile variable
In the past, header file paths were generated by the incdir command
Now they are generated by concatenating environment variables

In this way, when executing makefile, no shell command will be executed,
it will improve the speed of executing makfile
Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
2022-11-03 19:59:55 +08:00
Xiang Xiao 69575975f4 binfmt: elf_addrenv_free select addrenv before up_addrenv_destroy
to avoid up_addrenv_destroy delete the current task page table
this patch fix the below panic due to elf contain the unresolved symbol:
qemu-system-arm -s -S -semihosting -M sabrelite -m 1024 -smp 4 -kernel nuttx -nographic -net nic -net user,hostfwd=tcp:127.0.0.1:10023-10.0.2.15:23,hostfwd=tcp:127.0.0.1:10021-10.0.2.15:21,hostfwd=tcp:127.0.0.1:15001-10.0.2.15:5001,hostfwd=tcp:127.0.0.1:15555-10.0.2.15:5555
ABDGHIJKNOPQ
[    0.470000] elf_relocate: Section 2 reloc 163: Failed to get value of symbol[1505]: -2
[    0.530000] arm_dataabort: Data abort. PC: 108323de DFAR: 80202778 DFSR: 00000005
[    0.530000] up_assert: Assertion failed at file:armv7-a/arm_dataabort.c line: 161 task: /system/bin/init
[    0.530000] arm_registerdump: R0: 10845820 R1: 80202778 R2: 00000001  R3: 80202778
[    0.530000] arm_registerdump: R4: 80202624 R5: 80202634 R6: 80200330  FP: 80202628
[    0.530000] arm_registerdump: R8: 802026b4 SB: 00000000 SL: 8000dbfe R11: 00000000
[    0.530000] arm_registerdump: IP: 108453e0 SP: 108456b0 LR: 1082b091  PC: 108323de
[    0.530000] arm_registerdump: CPSR: a000007f
[    0.530000] arm_dump_stack: IRQ Stack:
[    0.530000] arm_dump_stack: sp:     10845510
[    0.530000] arm_dump_stack:   base: 108397c0
[    0.530000] arm_dump_stack:   size: 00000800
[    0.530000] arm_dump_stack: ERROR: IRQ Stack pointer is not within the stack
[    0.530000] arm_stackdump: 10839f00: deadbeef 00adbeef 00000001 10844990 00000000 00000000 80300000 1082f2ef
[    0.530000] arm_stackdump: 10839f20: deadbeef 00000000 deadbeef 10844990 80200000 1082f2ff 1083d980 1082f311
[    0.530000] arm_stackdump: 10839f40: 00000002 10814659 00000004 00000002 deadbeef 1081469b 00000001 00000000
[    0.530000] arm_stackdump: 10839f60: 10845460 00000057 10844a90 00000002 deadbeef 10808d1b 10845460 00000057
[    0.530000] arm_stackdump: 10839f80: 00dfbeef 00000057 00000000 00000002 deadbeef 10801d1f 10845460 00000057
[    0.530000] arm_stackdump: 10839fa0: 00000000 10800fc5 deadbeef 10845460 00000057 00000057 deadbeef 108000a0
[    0.530000] arm_dump_stack: User Stack:
[    0.530000] arm_dump_stack: sp:     108456b0
[    0.530000] arm_dump_stack:   base: 80202038
[    0.530000] arm_dump_stack:   size: 000007c8
[    0.530000] arm_dump_stack: ERROR: User Stack pointer is not within the stack
[    0.530000] arm_dump_stack: Kernel Stack:
[    0.530000] arm_dump_stack: sp:     108456b0
[    0.530000] arm_dump_stack:   base: 10844c10
[    0.530000] arm_dump_stack:   size: 00000c00
[    0.530000] arm_stackdump: 108456a0: 00000000 108453e0 108323de a000007f 80202778 10845820 00000000 10812aa9
[    0.530000] arm_stackdump: 108456c0: 00000000 1083ff90 10845838 00000000 001f001e 10812b2f 10840008 1083ff90
[    0.530000] arm_stackdump: 108456e0: 10845840 10812fab 10845840 1083ff90 005f0000 10844990 10845818 10845ff8
[    0.530000] arm_stackdump: 10845700: 10845ff8 10845838 10845840 10812017 fffffffe 10845820 80202624 1082b091
[    0.530000] arm_stackdump: 10845720: 00000000 00000000 80202778 10841d40 00000000 10845840 10845820 fffffffe
[    0.530000] arm_stackdump: 10845740: 00000000 1082998b 00000000 80200330 802026b4 80202778 10845788 0084578c
[    0.530000] arm_stackdump: 10845760: 10841d40 10844990 00000000 108306fb 00000000 80202634 802026b4 80202634
[    0.530000] arm_stackdump: 10845780: 80202778 80202628 00000000 00000000 00000000 00000000 00000000 1083085f
[    0.530000] arm_stackdump: 108457a0: 80200330 00000000 00000000 00000000 80202634 80202624 80202778 80202628
[    0.530000] arm_stackdump: 108457c0: 00000000 00000000 00000000 80202614 80202624 1080f26b 802026b4 80200330
[    0.530000] arm_stackdump: 108457e0: 80202624 80202778 80202628 0000001f 00000000 80202634 802026b4 108012c3
[    0.530000] arm_showtasks:    PID    PRI     STACK      USED   FILLED    COMMAND
[    0.530000] arm_showtasks:   ----   ----      2048       188     9.1%    irq
[    0.530000] arm_dump_task:      0      0      2024      1240    61.2%    Idle Task
[    0.530000] arm_dump_task:      1    224      1992       504    25.3%    hpwork 0x1083e0f8
[    0.530000] arm_dump_task:      2    100      1992       504    25.3%    lpwork 0x1083e110
after apply this patch, nsh report error instead of crash:
qemu-system-arm -s -S -semihosting -M sabrelite -m 1024 -smp 4 -kernel nuttx -nographic -net nic -net user,hostfwd=tcp:127.0.0.1:10023-10.0.2.15:23,hostfwd=tcp:127.0.0.1:10021-10.0.2.15:21,hostfwd=tcp:127.0.0.1:15001-10.0.2.15:5001,hostfwd=tcp:127.0.0.1:15555-10.0.2.15:5555
ABDGHIJKNOPQ
[    0.440000] elf_relocate: Section 2 reloc 163: Failed to get value of symbol[1505]: -2
telnetd [4:100]

NuttShell (NSH) NuttX-11.0.0
nsh> nsh: telnetd: command not found

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2022-11-03 00:08:12 +01:00
Xiang Xiao 64e7833cbc sched/spawn: Support task_spawnattr_[set|get]stackaddr
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2022-10-31 12:46:58 +09:00
Xiang Xiao b9b032af72 sched/spawn: Support task_spawnattr_[set|get]stacksize in kernel mode
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
Signed-off-by: chao an <anchao@xiaomi.com>
2022-10-31 12:46:58 +09:00
anjiahao d7b4e91dda Call nxsem_destroy or nxmutex_destry in the error path
1.Don't check the return value of nxsem_init or nxmutex_init
2.Fix some style issue

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2022-10-30 13:56:52 +01:00
Xiang Xiao 9f4bb7da97 binfmt: Let binfmt_copyargv return error code
so the caller can distinguish the empty argument and out of memory quickly

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
2022-10-19 08:22:56 +09:00
Ville Juven 7c003162a6 libelf/libelf_symbols.c: Fix compilation warning about void* arithmetics
Fixes minor issue with a trace (build failure with -Werror):
libelf/libelf_symbols.c:310:41: error: pointer of type 'void *' used in arithmetic [-Werror=pointer-arith]
  310 |               (uintptr_t)(sym->st_value + symbol->sym_value));
2022-09-28 20:35:31 +08:00
xiangdong6 4cd4303c32 binfmt: Check return pointer.
Function builtin_for_index may return NULL.
We must check this to prevent visit invalid address.

Signed-off-by: xiangdong6 <xiangdong6@xiaomi.com>
2022-09-03 19:39:51 +08:00
Masayuki Ishikawa 0cef7b765e binfmt: Fix memory leak in ELF loader
Summary:
- I noticed that the hello (ELF) application causes a memory leak.
- Finally, I found that the data section is not deallocated.
- This commit fixes this issue.

Impact:
- ELF loader with CONFIG_ARCH_ADDRENV=n

Testing:
- Tested with the following configs
  - sprensense:elf, esp32-devkitc:elf, sabre-6quad:elf
  - spresense:wifi_smp, rv-virt:nsh64, sabre-6quad:netnsh

Signed-off-by: Masayuki Ishikawa <Masayuki.Ishikawa@jp.sony.com>
2022-07-23 14:34:27 +08:00