mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 13:07:46 +00:00
Userland: chroot: Add --userspec
/-u
flag to set uid/gid for chroot
This commit is contained in:
parent
677af891b4
commit
f8c980a06b
2 changed files with 46 additions and 3 deletions
|
@ -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
|
## See also
|
||||||
|
|
||||||
* [`chroot`(2)](../man2/chroot.md)
|
* [`chroot`(2)](../man2/chroot.md)
|
||||||
|
|
|
@ -27,19 +27,40 @@
|
||||||
#include <AK/StringView.h>
|
#include <AK/StringView.h>
|
||||||
#include <LibCore/ArgsParser.h>
|
#include <LibCore/ArgsParser.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
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* path = nullptr;
|
||||||
const char* program = "/bin/Shell";
|
const char* program = "/bin/Shell";
|
||||||
int flags = -1;
|
const char* userspec = "0:0";
|
||||||
|
|
||||||
Core::ArgsParser args_parser;
|
Core::ArgsParser args_parser;
|
||||||
args_parser.add_positional_argument(path, "New root directory", "path");
|
args_parser.add_positional_argument(path, "New root directory", "path");
|
||||||
args_parser.add_positional_argument(program, "Program to run", "program", Core::ArgsParser::Required::No);
|
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,
|
true,
|
||||||
"Mount options",
|
"Mount options",
|
||||||
"options",
|
"options",
|
||||||
|
@ -69,7 +90,7 @@ int main(int argc, char** argv)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
args_parser.add_option(move(option));
|
args_parser.add_option(move(mount_options));
|
||||||
args_parser.parse(argc, argv);
|
args_parser.parse(argc, argv);
|
||||||
|
|
||||||
if (chroot_with_mount_flags(path, flags) < 0) {
|
if (chroot_with_mount_flags(path, flags) < 0) {
|
||||||
|
@ -82,6 +103,22 @@ int main(int argc, char** argv)
|
||||||
return 1;
|
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);
|
execl(program, program, nullptr);
|
||||||
perror("execl");
|
perror("execl");
|
||||||
return 1;
|
return 1;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue