37 lines
1.6 KiB
C
37 lines
1.6 KiB
C
/* PTY-only disposable executable. Never installed or selected by production. */
|
|
#define _GNU_SOURCE
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/ioctl.h>
|
|
#include <sys/prctl.h>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <errno.h>
|
|
#include <termios.h>
|
|
int main(int argc, char **argv) {
|
|
if (argc != 6 || strcmp(argv[1], "--archy-owned")) return 90;
|
|
struct stat tty, home;
|
|
int exclusive=0;
|
|
if (fstat(3,&tty) || fstat(4,&home) || !S_ISCHR(tty.st_mode) || !S_ISDIR(home.st_mode) ||
|
|
ioctl(3,TIOCGEXCL,&exclusive) || exclusive!=1 || getuid()!=53002 || geteuid()!=53002 ||
|
|
getgid()!=53002 || getegid()!=53002 || getgroups(0,NULL)!=0 || prctl(PR_GET_NO_NEW_PRIVS,0,0,0,0)!=1) return 91;
|
|
int escape=open(argv[3], O_RDWR|O_NONBLOCK|O_NOCTTY);
|
|
if (escape>=0 || (errno!=EBUSY && errno!=ENOENT && errno!=EACCES)) return 92;
|
|
int secret=open("/tmp/radio-custody-sentinel",O_WRONLY);
|
|
if(secret>=0 || errno!=EACCES) return 95;
|
|
if(symlinkat("/tmp/radio-custody-sentinel",8,"foreign-symlink")) return 96;
|
|
if (!strcmp(argv[2],"install")) {
|
|
pid_t child=fork();
|
|
if(child<0) return 93;
|
|
if(child==0) { setsid(); sleep(2); write(3,"ORPHAN",6); _exit(0); }
|
|
/* A successful leader cannot conceal a surviving writer. */
|
|
printf("descendant=%d\n",child); fflush(stdout); sleep(30); return 0;
|
|
}
|
|
if (write(3,"OWNED",5)!=5) return 94;
|
|
printf("{\"uid\":%u,\"inode\":%lu,\"rdev\":%lu,\"exclusive\":%d}\n",getuid(),tty.st_ino,tty.st_rdev,exclusive);
|
|
return 0;
|
|
}
|