바이너리의 SUID 비트가 여전히 "권한 거부" 오류를 생성합니다.

바이너리의 SUID 비트가 여전히 "권한 거부" 오류를 생성합니다.

alice모두 bobstaff그룹에 속합니다.

다음 폴더가 있습니다.

drwxrwxr-x 2 alice staff 4096 Oct 30 14:45 share

폴더에는 다음 파일이 있습니다.

-rwsr-xr-x 1 alice alice 8768 Oct 30 14:45 display-foo
-rw------- 1 alice alice 24 Oct 30 14:45 foo.txt

display-foo바이너리foo.txt파일, 기본적으로 명령을 사용하여 사용된 내용을 보여줍니다 cat.

실행 하면 alice바이너리가 성공적으로 실행되고 ./display-foo내용이 표시됩니다. foo.txt그러나 bobrun 을 실행 하면 ./display-foo바이너리가 성공적으로 실행되지만 내용은 foo.txt다음과 같습니다 .보여주지 마시오, 오류가 발생합니다 /bin/cat: /home/alice/share/foo.txt: Permission denied.


내가 이해한 바로는 setuid 비트를 설정하면 파일 소유자(이 경우)의 권한으로 바이너리 실행 파일을 실행할 수 있습니다 alice. 그렇다면 bob콘텐츠를 볼 수 없는 이유는 무엇입니까 foo.txt?


부록: 다시 컴파일했습니다바이너리geteuid()유효 사용자 ID(EUID)를 인쇄하는 기능을 사용하십시오 . 실제로 bob바이너리가 실행될 때 EUID는 alice. 그러나 허가가 거부되었습니다. 소스 코드 조각은 다음과 같습니다.

printf("%d", geteuid());
system("/bin/cat /home/alice/share/foo.txt");

답변1

나는 끝까지 읽었다매뉴얼 페이지system기능에 대해 설명을 제공하는 다음 단락이 발견되었습니다.

Do not use system() from a program with set-user-ID or set-group-ID
privileges, because strange values for some environment variables
might be used to subvert system integrity.  Use the exec(3) family of
functions instead, but not execlp(3) or execvp(3).  system() will
not, in fact, work properly from programs with set-user-ID or set-
group-ID privileges on systems on which /bin/sh is bash version 2,
since bash 2 drops privileges on startup.  (Debian uses a modified
bash which does not do this when invoked as sh.)

이 단락을 읽는 동안 읽은 다른 StackExchange 게시물은 다음과 같습니다.

후손을 위해, 그리고 주어진 예가 이 문제나 다른 상황에서 유사한 문제를 진단하는 데 도움이 될 수 있으므로 원래 예를 아래에 남겨 두었습니다.


버그가 발생했을 가능성이 있나요? Debian 9 및 gcc 6.3을 실행하는 Docker 컨테이너에서 문제를 재현할 수 없습니다. 귀하의 게시물에 설명된 장면을 재현하는 방법은 다음과 같습니다.

먼저 "alice" 및 "bob" 사용자를 생성하고 "staff" 그룹에 추가합니다.

useradd -m -G staff alice
useradd -m -G staff bob

다음으로 파일을 생성하고 소유권과 권한을 설정합니다.

# Create a subdirectory to hold the text file
sudo -u alice mkdir -p /home/alice/share/

# Create the text file
sudo -u alice bash -c 'echo "This is foo.txt" > /home/alice/share/foo.txt'

# Set restrictive permissions on the text file
chmod u=rw,g=,o= /home/alice/share/foo.txt

결과를 확인해 봅시다:

$ ls -l /home/alice/share/foo.txt
-rw------- 1 alice alice 16 Nov  4 15:33 /home/alice/share/foo.txt

display-foo이제 이 기능을 사용하는 버전을 만들어 보겠습니다 system.

$ cat <<HEREDOC > /usr/local/src/display-foo.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>

int main(int argc, char **argv) {

    struct passwd *r_pwd = getpwuid(getuid());
    printf("Real user: %s\n", r_pwd->pw_name);

    struct passwd *e_pwd = getpwuid(geteuid());
    printf("Effective user: %s\n", e_pwd->pw_name);

    system("/bin/cat /home/alice/share/foo.txt");
}
HEREDOC

$ gcc /usr/local/src/display-foo.c -o /usr/local/bin/display-foo

display-foosetuid 비트 설정을 포함하여 소유권과 권한을 설정해 보겠습니다 .

chown alice:alice /usr/local/bin/display-foo
chmod u=rwx,g=rx,o=rx /usr/local/bin/display-foo
chmod u+s /usr/local/bin/display-foo

결과도 확인해 보겠습니다.

$ ls -l /usr/local/bin/display-foo
-rwsr-xr-x 1 alice alice 8640 Nov  4 15:40 /usr/local/bin/display-foo

alice이제 우리는 다음과 같이 프로그램을 실행합니다 bob.

$ sudo -u alice display-foo

Real user: alice
Effective user: alice
This is foo.txt

$ sudo -u bob display-foo

Real user: bob
Effective user: alice
This is foo.txt

보시다시피 모든 것이 예상대로 작동하는 것 같습니다.

답변2

파일 foo.txt은 의 공유 디렉터리에 있으므로 Bob은 Alice home directory권한 문제로 인해 의 파일에 액세스할 수 없습니다. Alice home directory집에 디렉터리를 만들고 shared directory해당 디렉터리에 대한 액세스 권한을 부여할 수 있습니다.

또는 다음 명령을 사용하여 직원 shared directory과 함께 직책을 만들고 비트를 설정할 수 있습니다.groupownerSGID

chmod 2777 sharedDir

SGID이 비트가 디렉터리에 설정 되면 원래 디렉터리에 생성된 새 파일이나 하위 디렉터리는 사용자의 현재 기본 그룹을 기반으로 하는 대신 해당 디렉터리의 그룹 소유권을 상속합니다.

따라서 Alice여기서 파일이 생성되면 해당 파일의 그룹 소유권은직원그리고 그룹의 모든 구성원은 staff그룹 권한에 따라 이러한 파일에 액세스할 수 있습니다.

관련 정보