This repository has been archived by the owner on Jul 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPrivilege.py
88 lines (65 loc) · 1.48 KB
/
Privilege.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
'''Privilege module.
Control judge privileges.
Attributes:
JUDGE_UID (int): UID of judge account.
JUDGE_GID (int): GID of judge account.
NOBODY_YID (int): UID of nobody account.
NOBODY_GID (int): GID of nobody account.
'''
import os
import pwd
import contextlib
JUDGE_UID = None
JUDGE_GID = None
NOBODY_YID = None
NOBODY_GID = None
def init():
'''Initialize the module.'''
global JUDGE_UID
global JUDGE_GID
global NOBODY_YID
global NOBODY_GID
judge_pwd = pwd.getpwnam('judge')
nobody_pwd = pwd.getpwnam('nobody')
JUDGE_UID = judge_pwd[2]
JUDGE_GID = judge_pwd[3]
NOBODY_YID = nobody_pwd[2]
NOBODY_GID = nobody_pwd[3]
os.setgroups([])
drop(NOBODY_YID, NOBODY_GID)
def drop(uid, gid):
'''Drop privilege.
Args:
uid (int): UID to set.
gid (int): GID to set.
Returns:
None
'''
os.setegid(0)
os.seteuid(0)
os.setegid(gid)
os.seteuid(uid)
@contextlib.contextmanager
def fileaccess():
'''File access contextmanager.'''
old_euid = os.geteuid()
old_egid = os.getegid()
os.setegid(0)
os.seteuid(0)
os.setegid(JUDGE_GID)
os.seteuid(JUDGE_UID)
try:
yield
finally:
drop(old_euid, old_egid)
@contextlib.contextmanager
def fullaccess():
'''Full access contextmanager.'''
old_euid = os.geteuid()
old_egid = os.getegid()
os.setegid(0)
os.seteuid(0)
try:
yield
finally:
drop(old_euid, old_egid)