mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-20 17:58:18 +03:00
Userland: chroot: Add --userspec
/-u
flag to set uid/gid for chroot
This commit is contained in:
parent
677af891b4
commit
f8c980a06b
Notes:
sideshowbarker
2024-07-19 02:28:03 +09:00
Author: https://github.com/bcoles Commit: https://github.com/SerenityOS/serenity/commit/f8c980a06b3 Pull-request: https://github.com/SerenityOS/serenity/pull/4080
@ -24,6 +24,12 @@ Mount options can be given in the same format as for [`mount`(8)](mount.md).
|
||||
/
|
||||
```
|
||||
|
||||
```sh
|
||||
# chroot -u 200:200 /var/chroot
|
||||
$ id
|
||||
uid=200(nona) gid=200(n/a)
|
||||
```
|
||||
|
||||
## See also
|
||||
|
||||
* [`chroot`(2)](../man2/chroot.md)
|
||||
|
@ -27,19 +27,40 @@
|
||||
#include <AK/StringView.h>
|
||||
#include <LibCore/ArgsParser.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
int flags = -1;
|
||||
uid_t chroot_user = 0;
|
||||
gid_t chroot_group = 0;
|
||||
const char* path = nullptr;
|
||||
const char* program = "/bin/Shell";
|
||||
int flags = -1;
|
||||
const char* userspec = "0:0";
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_positional_argument(path, "New root directory", "path");
|
||||
args_parser.add_positional_argument(program, "Program to run", "program", Core::ArgsParser::Required::No);
|
||||
|
||||
Core::ArgsParser::Option option {
|
||||
Core::ArgsParser::Option userspec_option {
|
||||
true,
|
||||
"The uid:gid to use",
|
||||
"userspec",
|
||||
'u',
|
||||
"userpec",
|
||||
[&userspec](const char* s) {
|
||||
Vector<StringView> parts = StringView(s).split_view(':', true);
|
||||
if (parts.size() != 2)
|
||||
return false;
|
||||
userspec = s;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
args_parser.add_option(move(userspec_option));
|
||||
|
||||
Core::ArgsParser::Option mount_options {
|
||||
true,
|
||||
"Mount options",
|
||||
"options",
|
||||
@ -69,7 +90,7 @@ int main(int argc, char** argv)
|
||||
return true;
|
||||
}
|
||||
};
|
||||
args_parser.add_option(move(option));
|
||||
args_parser.add_option(move(mount_options));
|
||||
args_parser.parse(argc, argv);
|
||||
|
||||
if (chroot_with_mount_flags(path, flags) < 0) {
|
||||
@ -82,6 +103,22 @@ int main(int argc, char** argv)
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Failed parsing will silently fail open (uid=0; gid=0);
|
||||
// 0:0 is also the default when no --userspec argument is provided.
|
||||
auto parts = String(userspec).split(':', true);
|
||||
chroot_user = (uid_t)strtol(parts[0].characters(), nullptr, 10);
|
||||
chroot_group = (uid_t)strtol(parts[1].characters(), nullptr, 10);
|
||||
|
||||
if (setresgid(chroot_group, chroot_group, chroot_group)) {
|
||||
perror("setgid");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (setresuid(chroot_user, chroot_user, chroot_user)) {
|
||||
perror("setuid");
|
||||
return 1;
|
||||
}
|
||||
|
||||
execl(program, program, nullptr);
|
||||
perror("execl");
|
||||
return 1;
|
||||
|
Loading…
Reference in New Issue
Block a user