Skip to content

Instantly share code, notes, and snippets.

@nagayev
Last active April 14, 2022 21:46
Show Gist options
  • Save nagayev/d8908a78e752679c86131dd653e0c47a to your computer and use it in GitHub Desktop.
Save nagayev/d8908a78e752679c86131dd653e0c47a to your computer and use it in GitHub Desktop.
problem.c
#include <sys/syscall.h> /* Definition of SYS_* constants */
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <spawn.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#ifndef __NR_pidfd_open
#define __NR_pidfd_open 434 /* System call # on most architectures */
#endif
extern char** environ;
static int pidfd_open(pid_t pid, unsigned int flags)
{
return syscall(__NR_pidfd_open, pid, flags);
}
int main(int argc,char* argv[])
{
char* command = "echo 3 > lala.txt";
pid_t pid;
int status = posix_spawn(&pid,"/bin/sh",NULL,NULL,argv,environ);
printf("status: %i and %i\n",status,pid);
int pidfd = pidfd_open(pid, 0);
if (pidfd<0){
printf("panic: %i\n",errno);
return 1;
}
printf("pidfd: %i\n",pidfd);
write(pidfd,command,(strlen(command)+1));
close(pidfd);
system("ls");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment