Setuid-bit 和 apt-get 奇怪的行为
Setuid-bit and apt-get strange behavior
我的 raspberry pi 有一个奇怪的行为。
我写了一个小的 C 程序来更新树莓派。
它只是通过 system() "apt-get update && apt-get dist-upgrade && apt-get upgrade && rpi-update && reboot"
调用
使用 setuid 位,程序将 运行宁作为 root。
为什么是c程序?因为 setuid 位不适用于 bash 脚本。
作为超级用户没有问题。
如果我 运行 作为普通用户的程序,我在 "apt-get update" 之后遇到了这个错误(抱歉是德文版本):
E: Unable to write to /var/cache/apt/
E: The package lists or status file could not be parsed or opened.
setuid 位已设置,可执行文件的所有者是 root。
那么为什么我有另一种行为呢?
代码如下:
main.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main () {
int status = 0;
if (status == 0) {
status = system("apt-get -y update"); }
if (status == 0) {
status = system("apt-get -y dist-upgrade");
}
if (status == 0) {
status = system("apt-get -y upgrade");
}
if (status == 0) {
status = system("rpi-update");
}
if (status == 0) {
status = system("reboot");
}
return status;
}
生成文件:
CC=gcc
TARGET=update-system
PREFIX=/usr/bin
all: main.o
${CC} -o ${TARGET} main.c
make clean
main.o: main.c
${CC} -c main.c
.PHONY: clean
clean:
rm ./*.o
install:
chown root ${TARGET}
chgrp root ${TARGET}
chmod +s ${TARGET}
mv ${TARGET} ${PREFIX}
你需要 sudo:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void) {
int status = 0;
if (status == 0) {
status = system("sudo apt-get -y update"); }
if (status == 0) {
status = system("sudo apt-get -y dist-upgrade");
}
if (status == 0) {
status = system("sudo apt-get -y upgrade");
}
if (status == 0) {
status = system("sudo rpi-update");
}
if (status == 0) {
status = system("sudo reboot");
}
return status;
}
只需确保您的 用户 具有 root 权限。
当你的程序运行时,有效用户ID是root,但真正的用户ID是你自己。然后当你调用 system
时,真实有效的用户 ID 被 apt-get
继承。
似乎apt-get
在检查权限时看的是真实用户ID。所以需要在main
:
开头设置真实用户ID为root
setuid(0);
也就是说,使用 sudo
比 运行 setuid-root 程序更安全,因为您可以控制谁有权限这样做。
我的 raspberry pi 有一个奇怪的行为。
我写了一个小的 C 程序来更新树莓派。 它只是通过 system() "apt-get update && apt-get dist-upgrade && apt-get upgrade && rpi-update && reboot"
调用使用 setuid 位,程序将 运行宁作为 root。
为什么是c程序?因为 setuid 位不适用于 bash 脚本。
作为超级用户没有问题。
如果我 运行 作为普通用户的程序,我在 "apt-get update" 之后遇到了这个错误(抱歉是德文版本):
E: Unable to write to /var/cache/apt/
E: The package lists or status file could not be parsed or opened.
setuid 位已设置,可执行文件的所有者是 root。 那么为什么我有另一种行为呢?
代码如下:
main.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main () {
int status = 0;
if (status == 0) {
status = system("apt-get -y update"); }
if (status == 0) {
status = system("apt-get -y dist-upgrade");
}
if (status == 0) {
status = system("apt-get -y upgrade");
}
if (status == 0) {
status = system("rpi-update");
}
if (status == 0) {
status = system("reboot");
}
return status;
}
生成文件:
CC=gcc
TARGET=update-system
PREFIX=/usr/bin
all: main.o
${CC} -o ${TARGET} main.c
make clean
main.o: main.c
${CC} -c main.c
.PHONY: clean
clean:
rm ./*.o
install:
chown root ${TARGET}
chgrp root ${TARGET}
chmod +s ${TARGET}
mv ${TARGET} ${PREFIX}
你需要 sudo:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void) {
int status = 0;
if (status == 0) {
status = system("sudo apt-get -y update"); }
if (status == 0) {
status = system("sudo apt-get -y dist-upgrade");
}
if (status == 0) {
status = system("sudo apt-get -y upgrade");
}
if (status == 0) {
status = system("sudo rpi-update");
}
if (status == 0) {
status = system("sudo reboot");
}
return status;
}
只需确保您的 用户 具有 root 权限。
当你的程序运行时,有效用户ID是root,但真正的用户ID是你自己。然后当你调用 system
时,真实有效的用户 ID 被 apt-get
继承。
似乎apt-get
在检查权限时看的是真实用户ID。所以需要在main
:
setuid(0);
也就是说,使用 sudo
比 运行 setuid-root 程序更安全,因为您可以控制谁有权限这样做。