This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit d4986219e54d85fb7c58eadbe66a8453c498c587 Author: yushuailong <[email protected]> AuthorDate: Sat Sep 26 15:26:14 2026 +0800 sched/group: Preserve effective IDs in privileged setxid calls. When a privileged caller changes only its real user or group ID, setreuid() and setregid() must leave the corresponding effective ID unchanged. The current privileged paths instead copy the new real ID into both the effective and saved IDs. Route privileged callers through the common update path while retaining permission checks for unprivileged callers. Assisted-by: OpenAI Codex Signed-off-by: yushuailong <[email protected]> --- sched/group/group_setregid.c | 43 +++++++++++-------------------------------- sched/group/group_setreuid.c | 43 +++++++++++-------------------------------- 2 files changed, 22 insertions(+), 64 deletions(-) diff --git a/sched/group/group_setregid.c b/sched/group/group_setregid.c index 5ec7676d8b0..9fd21aa10a7 100644 --- a/sched/group/group_setregid.c +++ b/sched/group/group_setregid.c @@ -77,44 +77,23 @@ int setregid(gid_t rgid, gid_t egid) old_egid = rgroup->tg_egid; old_sgid = rgroup->tg_sgid; - if (old_egid == 0) - { - /* Super-user: may set any combination of real and effective IDs. */ + /* Non-super-user processes may only select existing credentials. */ - if (rgid != (gid_t)-1) + if (old_egid != 0) + { + if (rgid != (gid_t)-1 && + rgid != old_egid && rgid != old_sgid) { - rgroup->tg_gid = rgid; - - if (egid == (gid_t)-1) - { - rgroup->tg_egid = rgid; - rgroup->tg_sgid = rgid; - } + set_errno(EPERM); + return ERROR; } - if (egid != (gid_t)-1) + if (egid != (gid_t)-1 && + egid != old_egid && egid != old_sgid && egid != old_rgid) { - rgroup->tg_egid = egid; - rgroup->tg_sgid = egid; + set_errno(EPERM); + return ERROR; } - - return OK; - } - - /* Non-super-user */ - - if (rgid != (gid_t)-1 && - rgid != old_egid && rgid != old_sgid) - { - set_errno(EPERM); - return ERROR; - } - - if (egid != (gid_t)-1 && - egid != old_egid && egid != old_sgid && egid != old_rgid) - { - set_errno(EPERM); - return ERROR; } if (rgid != (gid_t)-1) diff --git a/sched/group/group_setreuid.c b/sched/group/group_setreuid.c index 1d37e070e2c..c423ad0489c 100644 --- a/sched/group/group_setreuid.c +++ b/sched/group/group_setreuid.c @@ -78,44 +78,23 @@ int setreuid(uid_t ruid, uid_t euid) old_euid = rgroup->tg_euid; old_suid = rgroup->tg_suid; - if (old_euid == 0) - { - /* Super-user: may set any combination of real and effective IDs. */ + /* Non-super-user processes may only select existing credentials. */ - if (ruid != (uid_t)-1) + if (old_euid != 0) + { + if (ruid != (uid_t)-1 && + ruid != old_euid && ruid != old_suid) { - rgroup->tg_uid = ruid; - - if (euid == (uid_t)-1) - { - rgroup->tg_euid = ruid; - rgroup->tg_suid = ruid; - } + set_errno(EPERM); + return ERROR; } - if (euid != (uid_t)-1) + if (euid != (uid_t)-1 && + euid != old_euid && euid != old_suid && euid != old_ruid) { - rgroup->tg_euid = euid; - rgroup->tg_suid = euid; + set_errno(EPERM); + return ERROR; } - - return OK; - } - - /* Non-super-user */ - - if (ruid != (uid_t)-1 && - ruid != old_euid && ruid != old_suid) - { - set_errno(EPERM); - return ERROR; - } - - if (euid != (uid_t)-1 && - euid != old_euid && euid != old_suid && euid != old_ruid) - { - set_errno(EPERM); - return ERROR; } if (ruid != (uid_t)-1)
