Last active
May 3, 2019 10:12
-
-
Save ak1t0/a6f926f599b3c7a30a4de027658d5bb4 to your computer and use it in GitHub Desktop.
memfd_create file less practice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
// for syscall() | |
#include <sys/syscall.h> | |
// for getpid() | |
#include <sys/types.h> | |
// for read() and execl() | |
#include <unistd.h> | |
int main(void) { | |
// init | |
pid_t pid; | |
pid = getpid(); | |
printf("start memfd_create\n"); | |
// create virtual fd | |
int virtual_fd; | |
virtual_fd = syscall(SYS_memfd_create, "kingyo", 0); | |
// error handling | |
if (virtual_fd == -1) { | |
perror("memdf_create fail\n"); | |
exit(1); | |
} | |
// display fd and proc info, and wait | |
printf("fd is %d\n", virtual_fd); | |
printf("check command:\n ls -l /proc/%d/fd\n", pid); | |
char ch; | |
ch = getchar(); | |
// write data want to execute | |
int r; | |
char *bin = "/bin/date"; | |
r = write(virtual_fd, bin, strlen(bin)); | |
if (r == -1) { | |
perror("write fail\n"); | |
exit(1); | |
} | |
// display fd info and wait | |
printf("data is written to fd\n", virtual_fd); | |
printf("check command:\n cat /proc/%d/fd/%d\n", pid, virtual_fd); | |
ch = getchar(); | |
// execute written data | |
char path[128] = {'\0'}; | |
sprintf(path, "/proc/%d/fd/%d", pid, virtual_fd); | |
int pfd; | |
pfd = open(path, 0); | |
if (pfd == -1) { | |
perror("open fail\n"); | |
} | |
char *buf[128] = {'\0'}; | |
//r = read(virtual_fd, buf, strlen(bin)) | |
r = read(pfd, buf, strlen(bin)); | |
if (r == -1) { | |
perror("read fail\n"); | |
exit(1); | |
} | |
execl(buf, buf, NULL); | |
perror("exec fail\n"); | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment