allow compilation of wrap.c with gcc

This commit is contained in:
MaxHearnden 2023-11-27 23:41:16 +00:00
parent f13aa2964a
commit d0635afc99

View file

@ -1,13 +1,22 @@
#define CLONE_NEWUSER 0x10000000 #define CLONE_NEWUSER 0x10000000
#define CLONE_NEWNS 0x00020000 #define CLONE_NEWNS 0x00020000
#define MS_BIND 4096 #define MS_BIND 4096
#define MNT_DETACH 0x00000002
#define _GNU_SOURCE
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <bootstrappable.h> #include <stdlib.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#ifdef __M2__
#include <bootstrappable.h>
int unshare(int flags) { int unshare(int flags) {
asm ( asm (
"lea_ebx,[esp+DWORD] %4" "lea_ebx,[esp+DWORD] %4"
@ -60,18 +69,40 @@ int chroot(char *path) {
); );
} }
int set_map(int parent_id, char *path) { #else
char *map_contents;
char *parent_id_str; // From bootstrappable.c in M2libc
void require(int bool, char* error)
{
if(!bool)
{
fputs(error, stderr);
exit(EXIT_FAILURE);
}
}
extern int unshare(int flags);
extern int mount(const char *source, const char *target,
const char *filesystemtype, unsigned long mountflags, const void *data);
#endif
void set_map(int parent_id, char *path) {
int fd = open(path, O_WRONLY, 0); int fd = open(path, O_WRONLY, 0);
require(fd != -1, "Cannot open map file"); require(fd != -1, "Cannot open map file");
map_contents = calloc(37, sizeof(char)); char *map_contents = calloc(38, sizeof(char));
#ifdef __M2__
strcpy(map_contents, "0 "); strcpy(map_contents, "0 ");
parent_id_str = int2str(parent_id, 10, 0); char *parent_id_str = int2str(parent_id, 10, 0);
strcat(map_contents, parent_id_str); strcat(map_contents, parent_id_str);
strcat(map_contents, " 1"); strcat(map_contents, " 1");
#else
snprintf(map_contents, 38, "0 %i 1", parent_id);
#endif
write(fd, map_contents, strlen(map_contents)); write(fd, map_contents, strlen(map_contents));
write(STDOUT_FILENO, map_contents, strlen(map_contents)); write(STDOUT_FILENO, map_contents, strlen(map_contents));
free(map_contents); free(map_contents);
@ -92,6 +123,7 @@ void deny_setgroups() {
} }
int main(int argc, char **argv, char **envp) { int main(int argc, char **argv, char **envp) {
require(argc > 1, "Expected at least one argument: command");
char *cwd = get_current_dir_name(); char *cwd = get_current_dir_name();
/* Do nothing if cwd is already root */ /* Do nothing if cwd is already root */
if (strcmp(cwd, "/")) { if (strcmp(cwd, "/")) {
@ -127,7 +159,7 @@ int main(int argc, char **argv, char **envp) {
mkdir ("sys", 0755); mkdir ("sys", 0755);
mount ("/sys", "sys", "", MS_BIND, NULL); mount ("/sys", "sys", "", MS_BIND, NULL);
mkdir ("tmp", 0755); mkdir ("tmp", 0755);
mkdir ("tmpfs", "tmp", "tmpfs", 0, NULL); mount ("tmpfs", "tmp", "tmpfs", 0, NULL);
chroot ("."); chroot (".");
} }
free(cwd); free(cwd);