Skip to content

Commit 6991b10

Browse files
authored
Merge pull request #946 from elicn/fix-root
Have the root property backed by OS logic
2 parents 87fc7a4 + fbd4f6a commit 6991b10

File tree

7 files changed

+32
-28
lines changed

7 files changed

+32
-28
lines changed

examples/netgear_6220_mips32el_linux.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def my_bind(ql: Qiling):
5858

5959
def my_netgear(path, rootfs):
6060
ql = Qiling(path, rootfs, verbose=QL_VERBOSE.DEBUG, profile="netgear_6220.ql", multithread=False)
61-
ql.root = False
61+
ql.os.root = False
6262

6363
ql.add_fs_mapper('/proc', '/proc')
6464
ql.set_syscall(4004, my_syscall_write)

qiling/core.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ def __init__(
9595
self._patch_lib = []
9696
self._debug_stop = False
9797
self._debugger = None
98-
self._root = False
9998

10099
###############################
101100
# Properties configured later #
@@ -574,19 +573,6 @@ def debugger(self) -> Union[str, bool]:
574573
def debugger(self, dbger):
575574
self._debugger = dbger
576575

577-
@property
578-
def root(self) -> bool:
579-
""" Whether run current program as root?
580-
581-
Type: bool
582-
Examples: ql.root = True
583-
"""
584-
return self._root
585-
586-
@root.setter
587-
def root(self, root):
588-
self._root = root
589-
590576
@property
591577
def filter(self) -> str:
592578
""" Filter logs with regex.

qiling/loader/elf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,9 @@ def __push_str(top: int, s: str) -> int:
364364
(AUX.AT_FLAGS, 0),
365365
(AUX.AT_ENTRY, elf_entry),
366366
(AUX.AT_UID, self.ql.os.uid),
367-
(AUX.AT_EUID, self.ql.os.uid),
367+
(AUX.AT_EUID, self.ql.os.euid),
368368
(AUX.AT_GID, self.ql.os.gid),
369-
(AUX.AT_EGID, self.ql.os.gid),
369+
(AUX.AT_EGID, self.ql.os.egid),
370370
(AUX.AT_HWCAP, elf_hwcap),
371371
(AUX.AT_CLKTCK, 100),
372372
(AUX.AT_RANDOM, randstraddr),

qiling/os/os.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,19 @@ def stdout(self, stream: TextIO) -> None:
123123
def stderr(self, stream: TextIO) -> None:
124124
self._stderr = stream
125125

126+
@property
127+
def root(self) -> bool:
128+
"""An indication whether the process is running as root.
129+
"""
130+
131+
# for this to work the os derivative should override this property
132+
# and implement the os logic. in case it is not, return False
133+
return False
134+
135+
@root.setter
136+
def root(self, enabled: bool) -> None:
137+
raise NotImplementedError('Running as root is not implemented for this OS')
138+
126139
def resolve_fcall_params(self, params: Mapping[str, Any]) -> Mapping[str, Any]:
127140
"""Transform function call raw parameters values into meaningful ones, according to
128141
their assigned type.

qiling/os/posix/posix.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,8 @@ def __init__(self, ql: Qiling):
5959
self.ql = ql
6060
self.sigaction_act = [0] * 256
6161

62-
if self.ql.root:
63-
self.uid = 0
64-
self.gid = 0
65-
else:
66-
self.uid = self.profile.getint("KERNEL","uid")
67-
self.gid = self.profile.getint("KERNEL","gid")
62+
self.uid = self.euid = self.profile.getint("KERNEL","uid")
63+
self.gid = self.egid = self.profile.getint("KERNEL","gid")
6864

6965
self.pid = self.profile.getint("KERNEL", "pid")
7066
self.ipv6 = self.profile.getboolean("NETWORK", "ipv6")
@@ -125,6 +121,15 @@ def stderr(self, stream: TextIO) -> None:
125121
self._stderr = stream
126122
self._fd[2] = stream
127123

124+
@QlOs.root.getter
125+
def root(self) -> bool:
126+
return (self.euid == 0) and (self.egid == 0)
127+
128+
@QlOs.root.setter
129+
def root(self, enabled: bool) -> None:
130+
self.euid = 0 if enabled else self.uid
131+
self.egid = 0 if enabled else self.gid
132+
128133
@property
129134
def syscall(self):
130135
return self.get_syscall()

qiling/os/posix/syscall/socket.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def ql_syscall_bind(ql: Qiling, bind_fd, bind_addr, bind_addrlen):
301301
port, host = struct.unpack(">HI", data[2:8])
302302
host = ql_bin_to_ip(host)
303303

304-
if ql.root == False and port <= 1024:
304+
if not ql.os.root and port <= 1024:
305305
port = port + 8000
306306

307307
if sin_family == 1:

qiling/os/posix/syscall/unistd.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def ql_syscall_issetugid(ql: Qiling):
6363

6464

6565
def ql_syscall_getuid(ql: Qiling):
66-
return 0
66+
return ql.os.uid
6767

6868

6969
def ql_syscall_getuid32(ql: Qiling):
@@ -75,15 +75,15 @@ def ql_syscall_getgid32(ql: Qiling):
7575

7676

7777
def ql_syscall_geteuid(ql: Qiling):
78-
return 0
78+
return ql.os.euid
7979

8080

8181
def ql_syscall_getegid(ql: Qiling):
82-
return 0
82+
return ql.os.egid
8383

8484

8585
def ql_syscall_getgid(ql: Qiling):
86-
return 0
86+
return ql.os.gid
8787

8888

8989
def ql_syscall_setgroups(ql: Qiling, gidsetsize: int, grouplist: int):

0 commit comments

Comments
 (0)