Skip to content

Instantly share code, notes, and snippets.

@brianv0
Created July 26, 2018 22:04
Show Gist options
  • Save brianv0/79f0d7f571688e82aadaaf8709af6c05 to your computer and use it in GitHub Desktop.
Save brianv0/79f0d7f571688e82aadaaf8709af6c05 to your computer and use it in GitHub Desktop.
fsuid test
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/syscall.h>
#define PARENTFILE ".parentfile"
#define CHILDFILE ".childfile"
#define gettid() syscall(SYS_gettid)
pthread_mutex_t kid1mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t kid1cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t kid2mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t kid2cond = PTHREAD_COND_INITIALIZER;
void *
kid1func (void *notused)
{
int fd;
(void)pthread_mutex_lock(&kid1mutex);
printf("kid1 (%d) setting fsuid to 9876\n",gettid());
(void)setfsuid(9876);
printf("kid1 (%d) creating %s\n",gettid(),CHILDFILE);
fd = open(CHILDFILE,O_RDWR|O_CREAT|O_EXCL,0600);
if (fd >= 0) {
printf("file created OK\n");
close(fd);
}
else {
perror("child create");
}
(void)pthread_cond_signal(&kid1cond);
(void)pthread_mutex_unlock(&kid1mutex);
return NULL;
}
void *
kid2func (void *notused)
{
int fd;
(void)pthread_mutex_lock(&kid2mutex);
printf("kid2 (%d) trying to open %s\n",gettid(),PARENTFILE);
fd = open(PARENTFILE,O_RDWR);
if (fd >= 0) {
printf("file opened, setfsuid must be per-thread\n");
close(fd);
}
else {
printf("open failed (%d), setfsuid must be per-process\n",
errno);
}
(void)pthread_cond_signal(&kid2cond);
(void)pthread_mutex_unlock(&kid2mutex);
return NULL;
}
int
main (int argc, char **argv)
{
int fd;
pthread_t kid1;
pthread_t kid2;
fd = open(PARENTFILE,O_RDWR|O_CREAT|O_EXCL,0600);
if (fd < 0) {
perror("parent create");
return !0;
}
(void)pthread_mutex_lock(&kid1mutex);
(void)pthread_create(&kid1,NULL,kid1func,NULL);
(void)pthread_mutex_lock(&kid2mutex);
(void)pthread_create(&kid2,NULL,kid2func,NULL);
printf("parent waking first child\n");
(void)pthread_cond_wait(&kid1cond,&kid1mutex);
(void)pthread_mutex_unlock(&kid1mutex);
printf("parent waking second child\n");
(void)pthread_cond_wait(&kid2cond,&kid2mutex);
(void)pthread_mutex_unlock(&kid2mutex);
printf("test complete\n");
(void)pthread_join(kid1,NULL);
(void)pthread_join(kid2,NULL);
return 0;
}
@brianv0
Copy link
Author

brianv0 commented Jul 26, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment