Compare commits

..

1 Commits

Author SHA1 Message Date
8798317ddc wip: more examples 2022-05-23 00:28:16 +01:00
3 changed files with 85 additions and 0 deletions

23
examples/dir_descriptor.c Normal file
View File

@ -0,0 +1,23 @@
#include <fcntl.h>
#include <linux/types.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
int main() {
int dirfd;
if ((dirfd = open("/tmp", __O_DIRECTORY)) < 0)
perror("opendir");
if (openat(dirfd, "filethatdoesexist", O_RDONLY) < 0)
perror("openat0");
if (chroot("/tmp"))
perror("chroot");
if (open("/etc/passwd", O_RDONLY) < 0)
perror("open");
if (openat(dirfd, "../etc/passwd", O_RDONLY) < 0)
perror("openat1");
if (openat(dirfd, "filethatdoesexist", O_RDONLY) < 0)
perror("openat2");
}

View File

@ -0,0 +1,42 @@
#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#define BUF_SIZE 1024
void print_file(int fd);
int main() {
int fd;
if ((fd = open("/etc/passwd", O_RDONLY)) < 0)
perror("open");
print_file(fd);
if (close(fd))
perror("close");
if (unshare(CLONE_NEWNS))
perror("unshare");
printf("----- unshared -----\n");
if ((fd = open("/etc/passwd", O_RDONLY)) < 0)
perror("open");
print_file(fd);
if (close(fd))
perror("close");
}
void print_file(int fd) {
char buf[BUF_SIZE];
int bytes_read;
while ((bytes_read = read(fd, buf, BUF_SIZE)) > 0)
if (write(1, buf, bytes_read) < 0)
perror("write");
if (bytes_read == -1)
perror("read");
}

20
examples/unshare_user.c Normal file
View File

@ -0,0 +1,20 @@
#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main() {
int fd;
if (unshare(CLONE_NEWUSER))
perror("unshare");
if ((fd = open("/proc/self/uid_map", O_WRONLY)) == -1)
perror("open");
if (write(fd, "0 0 4294967295\n", 15) == -1)
perror("write");
if (execl("/bin/bash", "/bin/bash", NULL))
perror("execl");
}