如何从源代码构建“su”?

尝试在Ubuntu 20.04上构建自定义的"su",但从原始源代码开始进行测试(不做更改)。 运行
apt-get source login

我得到的是shadow-4.8.1目录。运行它。
./configure
make

尝试运行
src/su testuser

它不起作用!认证失败错误。运行原始操作系统二进制文件:
/usr/bin/su testuser

正常运行! 什么鬼?

我觉得构建过程中缺少一些模块/参数。

查看影子软件包的构建日志。

https://launchpad.net/ubuntu/+source/shadow/1:4.11.1+dfsg1-2ubuntu1/+build/23780688

看下一个./configure字符串。
./configure --build=x86_64-linux-gnu --prefix=/usr --includedir=\${prefix}/include --mandir=\${prefix}/share/man --infodir=\${prefix}/share/info --sysconfdir=/etc --localstatedir=/var --disable-silent-rules --libdir=\${prefix}/lib/x86_64-linux-gnu --runstatedir=/run --disable-maintainer-mode --disable-dependency-tracking --disable-shared --without-libcrack --mandir=/usr/share/man --with-libpam --enable-shadowgrp --enable-man --disable-account-tools-setuid --with-group-name-max-length=32 --without-acl --without-attr --without-su --without-tcb 

看那里:
--without-su

尝试使用此 ./configure 字符串,它确实生成所有二进制文件而不需要 "su"。尝试替换它为

--with-su

新的二进制文件也不起作用。认证失败。:(
关于此事有两个问题。
1. 我在哪里可以找到原始“su”的构建日志? 2. 为什么新构建的“su”无法看到真实的操作系统用户,而这些用户存在于/etc/shadow|passwd中?

4FYI,Ubuntu 20.04中su的默认实现由util-linux软件包提供。 - steeldriver
1个回答

在编译过程中没有出现任何问题。但是你编译的“su”命令属于你的用户,并且没有设置set uid位,这意味着它以你的用户身份执行。
你的用户没有切换到其他用户ID的能力。这是为root保留的。
如果你运行“ls -l /usr/bin/su”命令,你会看到它属于root并且设置了suid位。
$ ls -l /usr/bin/su
-rwsr-xr-x 1 root root 55480 Apr  1 21:16 /usr/bin/su

你可以通过运行chown root:root /path/to/your/su将其所有者更改为root,然后运行chmod 4755 /path/to/your/su将其设置为setuid来解决这个问题。