Created
April 18, 2017 19:01
-
-
Save tixxdz/ed1ed92b890cc7fd268b5bdcf578c460 to your computer and use it in GitHub Desktop.
pr_modules_autoload.c
This file contains 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 <errno.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <linux/prctl.h> | |
#include <sys/prctl.h> | |
#include <sys/ptrace.h> | |
extern char **environ; | |
static char *args[] = { "/bin/bash", NULL }; | |
#ifndef PR_SET_MODULES_AUTOLOAD | |
#define PR_SET_MODULES_AUTOLOAD 48 | |
#define PR_GET_MODULES_AUTOLOAD 49 | |
#endif | |
int main(int argc, const char **argv) | |
{ | |
int ret; | |
ret = prctl(PR_GET_MODULES_AUTOLOAD, 0, 0, 0, 0); | |
if (ret < 0) { | |
fprintf(stderr, "Error PR_GET_MODULES_AUTOLOAD failed: %d (%m)\n", -errno); | |
return EXIT_FAILURE; | |
} | |
ret = prctl(PR_SET_MODULES_AUTOLOAD, 1, 0, 0, 0); | |
if (ret < 0) { | |
fprintf(stderr, "Error PR_SET_MODULES_AUTOLOAD to 1 failed: %d (%m)\n", -errno); | |
return EXIT_FAILURE; | |
} | |
/* Pass extra non-zero argument */ | |
ret = prctl(PR_GET_MODULES_AUTOLOAD, 1, 0, 0, 0); | |
if (ret >= 0 || errno != EINVAL) { | |
printf("Error PR_GET_MODULES_AUTOLOAD should fail with -EINVAL"); | |
return EXIT_FAILURE; | |
} | |
ret = prctl(PR_GET_MODULES_AUTOLOAD, 0, 0, 0, 0); | |
if (ret < 0) { | |
fprintf(stderr, "Error PR_GET_MODULES_AUTOLOAD failed: %d (%m)\n", -errno); | |
return EXIT_FAILURE; | |
} | |
printf(" task modules_autoload: %d\n", ret); | |
if (ret != 1) { | |
fprintf(stderr, "Error PR_GET_MODULES_AUTOLOAD should return 1\n"); | |
return EXIT_FAILURE; | |
} | |
/* Should fail with -EPERM */ | |
ret = prctl(PR_SET_MODULES_AUTOLOAD, 0, 0, 0, 0); | |
if (ret < 0) { | |
fprintf(stderr, "PR_SET_MODULES_AUTOLOAD to 0 failed: %d (%m)\n", -errno); | |
} else { | |
fprintf(stderr, "Error PR_SET_MODULES_AUTOLOAD to 0 succeeded\n"); | |
return EXIT_FAILURE; | |
} | |
ret = prctl(PR_SET_MODULES_AUTOLOAD, 2, 0, 0, 0); | |
if (ret < 0) { | |
fprintf(stderr, "Error PR_SET_MODULES_AUTOLOAD to 2 failed: %d (%m)\n", -errno); | |
return EXIT_FAILURE; | |
} | |
ret = prctl(PR_GET_MODULES_AUTOLOAD, 0, 0, 0, 0); | |
if (ret < 0) { | |
fprintf(stderr, "Error PR_GET_MODULES_AUTOLOAD failed: %d (%m)\n", -errno); | |
return EXIT_FAILURE; | |
} | |
printf(" task modules_autoload: %d\n", ret); | |
execv(args[0], args); | |
fprintf(stderr, "error on execve(): %d (%m)\n", -errno); | |
exit(EXIT_FAILURE); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment