簡體   English   中英

Setuid位和apt-get奇怪的行為

[英]Setuid-bit and apt-get strange behavior

我在樹莓派上有一個奇怪的舉動。

我寫了一個小的C程序來更新RPi。 它只是通過system()來調用“ apt-get update && apt-get dist-upgrade && apt-get upgrade && rpi-update && reboot”

使用setuid位,程序將以root身份運行。

為什么要使用AC程序? 因為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}

當您的程序運行時,有效的用戶ID是root,但實際的用戶ID是您自己。 然后,當您調用systemapt-get會繼承真實有效的用戶ID。

似乎apt-get在檢查權限時會查看真實的用戶ID。 因此,您需要在main的開頭將真實用戶ID設置為root:

setuid(0);

話雖這么說,使用sudo比運行setuid-root程序更安全,因為您可以控制誰有權這樣做。

您需要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特權即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM