unix - 为什么如果在 passwd 上设置了 suid 位,普通用户无法更改其他人的密码

标签 unix

根据 suid 概念,如果在任何文件/可执行文件上设置了 suid 位,并且普通用户执行该命令。命令将以该文件/命令的所有者的权限执行。

suid 位在“passwd”命令上设置,root 是 passwd 的所有者。现在,如果普通用户说“user1”执行命令“passwd”,它将以root权限执行。但是,他只能更改自己的密码,而不能更改其他人的密码。为什么这样?

最佳答案

/usr/bin/passwd 程序将检查两个条件:

  1. 当前真实用户ID是否等于密码项/条目的UID;
  2. 当前用户是否为 root。

只有满足两个条件之一,进程才能修改密码。

因此,阻止你修改其他用户密码的是/usr/bin/passwd程序,而不是suid权限。您对 suid 权限的理解是正确的。

请参阅link 。以下是 Jjanel 的回答:

google: passwd.c

https://github.com/shadow-maint/shadow/blob/master/src/passwd.c

see lines 1056-1069:

Code:

/*
     * If the UID of the user does not match the current real UID,
     * check if I'm root.
     */
    if (!amroot && (pw->pw_uid != getuid ())) {
        (void) fprintf (stderr,
                        _("%s: You may not view or modify password information for %s.\n"),
                        Prog, name);
        SYSLOG ((LOG_WARN,
                 "%s: can't view or modify password information for %s",
                 Prog, name));
        closelog ();
        exit (E_NOPERM);
}

顺便说一句,请原谅我糟糕的英语,我正在寻找提高英语水平的好方法。

关于unix - 为什么如果在 passwd 上设置了 suid 位,普通用户无法更改其他人的密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27015968/

相关文章:

linux - Shell 脚本 - 如果变量为 null 或为空则退出脚本

linux - Unix权限设置速度

linux - 函数在脚本 shell 中不起作用

linux - 检查目录是否存在且可访问

linux - grep -v '.*' 不起作用

shell - Unix shell 标志

bash - 在 bash 中确定 rdf mime 类型

python - 为python3.3安装pip

shell - 如何在 Unix 中根据分隔符将一个文件拆分为多个文件,并删除分隔符

c - 在 C 中使用管道在另一个程序中使用 cat 输出