1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 04:57:45 +00:00

Kernel+Userland: Implement setuid() and setgid() and add /bin/su

Also show setuid and setgid bits in "ls -l" output. :^)
This commit is contained in:
Andreas Kling 2019-02-21 23:35:07 +01:00
parent 6071a77e8e
commit 920e8e58ed
9 changed files with 79 additions and 8 deletions

1
Userland/.gitignore vendored
View file

@ -33,3 +33,4 @@ chmod
pape
ln
df
su

View file

@ -29,6 +29,7 @@ OBJS = \
top.o \
df.o \
ln.o \
su.o \
rm.o
APPS = \
@ -63,6 +64,7 @@ APPS = \
top \
ln \
df \
su \
rm
ARCH_FLAGS =
@ -179,6 +181,9 @@ ln: ln.o
df: df.o
$(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a
su: su.o
$(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a
.cpp.o:
@echo "CXX $<"; $(CXX) $(CXXFLAGS) -o $@ -c $<

View file

@ -148,10 +148,10 @@ int do_dir(const char* path)
printf("%c%c%c%c%c%c%c%c",
st.st_mode & S_IRUSR ? 'r' : '-',
st.st_mode & S_IWUSR ? 'w' : '-',
st.st_mode & S_IXUSR ? 'x' : '-',
st.st_mode & S_ISUID ? 's' : (st.st_mode & S_IXUSR ? 'x' : '-'),
st.st_mode & S_IRGRP ? 'r' : '-',
st.st_mode & S_IWGRP ? 'w' : '-',
st.st_mode & S_IXGRP ? 'x' : '-',
st.st_mode & S_ISGID ? 's' : (st.st_mode & S_IXGRP ? 'x' : '-'),
st.st_mode & S_IROTH ? 'r' : '-',
st.st_mode & S_IWOTH ? 'w' : '-'
);

44
Userland/su.cpp Normal file
View file

@ -0,0 +1,44 @@
#include <unistd.h>
#include <stdio.h>
#include <pwd.h>
#include <grp.h>
#include <alloca.h>
extern "C" int main(int, char**);
int main(int argc, char** argv)
{
uid_t uid;
gid_t gid;
if (argc == 1) {
uid = 0;
gid = 0;
} else {
auto* pwd = getpwnam(argv[1]);
if (!pwd) {
fprintf(stderr, "No such user: %s\n", argv[1]);
return 1;
}
uid = pwd->pw_uid;
gid = pwd->pw_gid;
}
int rc = setgid(uid);
if (rc < 0) {
perror("setgid");
return 1;
}
rc = setuid(gid);
if (rc < 0) {
perror("setuid");
return 1;
}
rc = execl("/bin/sh", "sh", nullptr);
if (rc < 0) {
perror("execl");
return 1;
}
return 0;
}