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>
This commit is contained in:
fangxinyong
2023-08-11 20:09:21 +08:00
committed by Xiang Xiao
parent cd5ee3fb07
commit 2d73e86b47
5 changed files with 40 additions and 8 deletions
+13
View File
@@ -24,6 +24,7 @@
#include <nuttx/config.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdint.h>
#include <stdlib.h>
@@ -279,6 +280,18 @@ int exec_module(FAR struct binary_s *binp,
pid = tcb->cmn.pid;
#ifdef CONFIG_SCHED_USER_IDENTITY
if (binp->mode & S_ISUID)
{
tcb->cmn.group->tg_euid = binp->uid;
}
if (binp->mode & S_ISGID)
{
tcb->cmn.group->tg_egid = binp->gid;
}
#endif
/* Then activate the task at the provided priority */
nxtask_activate((FAR struct tcb_s *)tcb);