-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tomasz bla Fortuna
committed
Dec 23, 2009
1 parent
62fdf48
commit d7d40e3
Showing
7 changed files
with
479 additions
and
41 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <sys/types.h> | ||
#include <sys/stat.h> | ||
#include <fcntl.h> | ||
#include <unistd.h> | ||
#include <errno.h> | ||
|
||
#define LOCK_FILE ".otpasswd.lck" | ||
int fd = -1; | ||
|
||
int lock() | ||
{ | ||
struct flock fl; | ||
|
||
fl.l_type = F_WRLCK; | ||
fl.l_whence = SEEK_SET; | ||
fl.l_start = fl.l_len = 0; | ||
|
||
fd = open(LOCK_FILE, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR); | ||
|
||
if (fd == -1) { | ||
/* Unable to create file, therefore unable to obtain lock */ | ||
perror("open"); | ||
printf("Unable to open a state file\n"); | ||
return 1; | ||
} | ||
|
||
if (fcntl(fd, F_SETLK, &fl) == 0) { | ||
printf("Locked\n"); | ||
return 0; | ||
} | ||
|
||
close(fd); | ||
printf("Unable to lock\n"); | ||
return 1; | ||
} | ||
|
||
void overwrite() | ||
{ | ||
FILE *f = fopen(LOCK_FILE, "w"); | ||
if (!f) { | ||
printf("Unable to open for overwrite\n"); | ||
return; | ||
} | ||
fprintf(f, "Dupablada\n"); | ||
if (fflush(f) != 0) { | ||
printf("Unable to fflush\n"); | ||
} | ||
fclose(f); | ||
} | ||
|
||
int unlock() | ||
{ | ||
struct flock fl; | ||
|
||
if (fd < 0) { | ||
printf("No lock to release!\n"); | ||
return 1; | ||
} | ||
|
||
fl.l_type = F_UNLCK; | ||
fl.l_whence = SEEK_SET; | ||
fl.l_start = fl.l_len = 0; | ||
|
||
int ret = fcntl(fd, F_SETLK, &fl); | ||
|
||
close(fd); | ||
fd = -1; | ||
|
||
if (ret != 0) { | ||
printf("Strange error while releasing lock\n"); | ||
/* Strange error while releasing the lock */ | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
if (lock() != 0) { | ||
return 1; | ||
} | ||
|
||
// overwrite(); | ||
printf("Waiting for keypress\n"); getchar(); | ||
unlock(); | ||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
|
||
#define _GNU_SOURCE /* For setres* */ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#include <unistd.h> | ||
|
||
static int real_uid; | ||
static int set_uid; | ||
|
||
static int real_gid; | ||
static int set_gid; | ||
|
||
void init() | ||
{ | ||
real_uid = getuid(); | ||
set_uid = geteuid(); | ||
|
||
real_gid = getgid(); | ||
set_gid = getegid(); | ||
} | ||
|
||
void print(void) | ||
{ | ||
int uid, euid; | ||
int gid, egid; | ||
|
||
uid = getuid(); | ||
euid = geteuid(); | ||
|
||
gid = getgid(); | ||
egid = getegid(); | ||
|
||
printf("UID/eUID: %d/%d GID/eGID: %d/%d\n", uid, euid, gid, egid); | ||
} | ||
|
||
|
||
void check_perms(void) | ||
{ | ||
printf("Checking root perms! "); | ||
print(); | ||
|
||
FILE *f = fopen("/etc/shadow", "r"); | ||
if (!f) { | ||
printf("We do not have root access permissions\n"); | ||
} else { | ||
fclose(f); | ||
printf("Root permissions\n"); | ||
} | ||
} | ||
|
||
void drop_temporarily(void) | ||
{ | ||
/* On systems without setres* use setre*. But make sure it works */ | ||
const int gid = getgid(), uid = getuid(); | ||
const int egid = getegid(), euid = geteuid(); | ||
|
||
if (setresuid(uid, uid, euid) != 0) | ||
goto error; | ||
if (setresgid(gid, gid, egid) != 0) | ||
goto error; | ||
|
||
/* Paranoid check */ | ||
if (geteuid() != getuid() || getegid() != getgid()) { | ||
printf("d_t: fun\n"); | ||
goto error; | ||
} | ||
return; | ||
error: | ||
printf("d_t: failure\n"); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
void drop_pernamently(void) | ||
{ | ||
/* On systems without setres* use setre*. But make sure it works */ | ||
const int gid = getgid(), uid = getuid(); | ||
|
||
if (setresuid(uid, uid, uid) != 0) | ||
goto error; | ||
if (setresgid(gid, gid, gid) != 0) | ||
goto error; | ||
|
||
/* Paranoid check */ | ||
if (geteuid() != getuid() || getegid() != getgid()) { | ||
printf("d_t: fun\n"); | ||
goto error; | ||
} | ||
|
||
return; | ||
error: | ||
printf("d_p: failure\n"); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
void restore(void) | ||
{ | ||
/* On systems without setres* use setre*. But make sure it works */ | ||
|
||
/* 0 should be remembered before! */ | ||
if (setresuid(real_uid, set_uid, set_uid) != 0) | ||
goto error; | ||
if (setresgid(real_gid, set_gid, set_gid) != 0) | ||
goto error; | ||
|
||
/* Paranoid check */ | ||
if (geteuid() != set_uid || getegid() != set_gid) { | ||
printf("d_t: fun\n"); | ||
goto error; | ||
} | ||
|
||
return; | ||
error: | ||
printf("d_p: failure\n"); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
clearenv(); | ||
|
||
init(); | ||
|
||
printf("Initial: "); | ||
print(); | ||
|
||
check_perms(); | ||
|
||
printf("* TEMPORARY DROP \n"); | ||
drop_temporarily(); | ||
|
||
check_perms(); | ||
|
||
printf("* RESTORE \n"); | ||
restore(); | ||
|
||
check_perms(); | ||
|
||
printf("* PERNAMENT DROP \n"); | ||
drop_pernamently(); | ||
|
||
check_perms(); | ||
|
||
|
||
printf("* RESTORE (we should fail now) \n"); | ||
restore(); | ||
|
||
check_perms(); | ||
|
||
|
||
return 0; | ||
} |
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
Oops, something went wrong.