33# Cross Platform and Multi Architecture Advanced Binary Emulation Framework
44#
55
6+ import os
7+
68from qiling import Qiling
7- from qiling .const import *
8- from qiling .os .linux .thread import *
9- from qiling .os .posix .filestruct import *
10- from qiling .os .filestruct import *
9+ from qiling .const import QL_OS , QL_ARCH
10+ from qiling .exception import QlSyscallError
1111from qiling .os .posix .const import *
12- from qiling .os .posix .const_mapping import *
13- from qiling .exception import *
12+ from qiling .os .posix .const_mapping import ql_open_flag_mapping , open_flags_mapping
13+ from qiling .os . posix . filestruct import ql_socket
1414
15- def ql_syscall_open (ql : Qiling , filename , flags , mode , * args , ** kw ):
15+ def ql_syscall_open (ql : Qiling , filename : int , flags : int , mode : int ):
1616 path = ql .os .utils .read_cstring (filename )
1717 real_path = ql .os .path .transform_to_real_path (path )
1818 relative_path = ql .os .path .transform_to_relative_path (path )
@@ -38,12 +38,13 @@ def ql_syscall_open(ql: Qiling, filename, flags, mode, *args, **kw):
3838 ql .log .debug ("open(%s, %s, 0o%o) = %d" % (relative_path , open_flags_mapping (flags , ql .archtype ), mode , regreturn ))
3939
4040 if regreturn >= 0 and regreturn != 2 :
41- ql .log .debug (" File Found: %s" % real_path )
41+ ql .log .debug (f' File found: { real_path :s } ' )
4242 else :
43- ql .log .debug ("File Not Found %s" % real_path )
43+ ql .log .debug (f'File not found { real_path :s} ' )
44+
4445 return regreturn
4546
46- def ql_syscall_creat (ql : Qiling , filename , mode , * args , ** kw ):
47+ def ql_syscall_creat (ql : Qiling , filename : int , mode : int ):
4748 flags = linux_open_flags ["O_WRONLY" ] | linux_open_flags ["O_CREAT" ] | linux_open_flags ["O_TRUNC" ]
4849
4950 path = ql .os .utils .read_cstring (filename )
@@ -71,13 +72,14 @@ def ql_syscall_creat(ql: Qiling, filename, mode, *args, **kw):
7172 ql .log .debug ("creat(%s, %s, 0o%o) = %d" % (relative_path , open_flags_mapping (flags , ql .archtype ), mode , regreturn ))
7273
7374 if regreturn >= 0 and regreturn != 2 :
74- ql .log .debug (" File Found: %s" % real_path )
75+ ql .log .debug (f' File found: { real_path :s } ' )
7576 else :
76- ql .log .debug ("File Not Found %s" % real_path )
77+ ql .log .debug (f'File not found { real_path :s} ' )
78+
7779 return regreturn
7880
79- def ql_syscall_openat (ql : Qiling , fd , path , flags , mode , * args , ** kw ):
80- path = ql .os .utils .read_cstring (path )
81+ def ql_syscall_openat (ql : Qiling , fd : int , path : int , flags : int , mode : int ):
82+ file_path = ql .os .utils .read_cstring (path )
8183 # real_path = ql.os.path.transform_to_real_path(path)
8284 # relative_path = ql.os.path.transform_to_relative_path(path)
8385
@@ -99,28 +101,27 @@ def ql_syscall_openat(ql: Qiling, fd, path, flags, mode, *args, **kw):
99101 except :
100102 dir_fd = None
101103
102- ql .os .fd [idx ] = ql .os .fs_mapper .open_ql_file (path , flags , mode , dir_fd )
104+ ql .os .fd [idx ] = ql .os .fs_mapper .open_ql_file (file_path , flags , mode , dir_fd )
103105 regreturn = idx
104106 except QlSyscallError as e :
105107 regreturn = - e .errno
106108
107- ql .log .debug (f'openat(fd = { fd :d} , path = { path } , flags = { open_flags_mapping (flags , ql .archtype )} , mode = { mode :#o} ) = { regreturn :d} ' )
109+ ql .log .debug (f'openat(fd = { fd :d} , path = { file_path } , flags = { open_flags_mapping (flags , ql .archtype )} , mode = { mode :#o} ) = { regreturn :d} ' )
108110
109111 return regreturn
110112
111113
112- def ql_syscall_fcntl (ql : Qiling , fcntl_fd , fcntl_cmd , fcntl_arg , * args , ** kw ):
113- if not (0 <= fcntl_fd < NR_OPEN ) or \
114- ql .os .fd [fcntl_fd ] == 0 :
114+ def ql_syscall_fcntl (ql : Qiling , fd : int , cmd : int , arg : int ):
115+ if not (0 <= fd < NR_OPEN ) or ql .os .fd [fd ] == 0 :
115116 return - EBADF
116117
117- f = ql .os .fd [fcntl_fd ]
118-
119- if fcntl_cmd == F_DUPFD :
120- if 0 <= fcntl_arg < NR_OPEN :
118+ f = ql .os .fd [fd ]
119+
120+ if cmd == F_DUPFD :
121+ if 0 <= arg < NR_OPEN :
121122 for idx , val in enumerate (ql .os .fd ):
122- if val == 0 and idx >= fcntl_arg :
123- new_fd = ql .os .fd [fcntl_fd ].dup ()
123+ if val == 0 and idx >= arg :
124+ new_fd = ql .os .fd [fd ].dup ()
124125 ql .os .fd [idx ] = new_fd
125126 regreturn = idx
126127 break
@@ -129,18 +130,18 @@ def ql_syscall_fcntl(ql: Qiling, fcntl_fd, fcntl_cmd, fcntl_arg, *args, **kw):
129130 else :
130131 regreturn = - EINVAL
131132
132- elif fcntl_cmd == F_GETFD :
133+ elif cmd == F_GETFD :
133134 regreturn = getattr (f , "close_on_exec" , 0 )
134135
135- elif fcntl_cmd == F_SETFD :
136- f .close_on_exec = 1 if fcntl_arg & FD_CLOEXEC else 0
136+ elif cmd == F_SETFD :
137+ f .close_on_exec = 1 if arg & FD_CLOEXEC else 0
137138 regreturn = 0
138139
139- elif fcntl_cmd == F_GETFL :
140- regreturn = ql .os .fd [fcntl_fd ].fcntl (fcntl_cmd , fcntl_arg )
140+ elif cmd == F_GETFL :
141+ regreturn = ql .os .fd [fd ].fcntl (cmd , arg )
141142
142- elif fcntl_cmd == F_SETFL :
143- ql .os .fd [fcntl_fd ].fcntl (fcntl_cmd , fcntl_arg )
143+ elif cmd == F_SETFL :
144+ ql .os .fd [fd ].fcntl (cmd , arg )
144145 regreturn = 0
145146
146147 else :
@@ -149,45 +150,50 @@ def ql_syscall_fcntl(ql: Qiling, fcntl_fd, fcntl_cmd, fcntl_arg, *args, **kw):
149150 return regreturn
150151
151152
152- def ql_syscall_fcntl64 (ql : Qiling , fcntl_fd , fcntl_cmd , fcntl_arg , * args , ** kw ):
153+ def ql_syscall_fcntl64 (ql : Qiling , fd : int , cmd : int , arg : int ):
153154
154155 # https://linux.die.net/man/2/fcntl64
155- if fcntl_cmd == F_DUPFD :
156- if 0 <= fcntl_arg < NR_OPEN and 0 <= fcntl_fd < NR_OPEN :
157- if ql .os .fd [fcntl_fd ] != 0 :
158- new_fd = ql .os .fd [fcntl_fd ].dup ()
156+ if cmd == F_DUPFD :
157+ if 0 <= arg < NR_OPEN and 0 <= fd < NR_OPEN :
158+ if ql .os .fd [fd ] != 0 :
159+ new_fd = ql .os .fd [fd ].dup ()
159160 for idx , val in enumerate (ql .os .fd ):
160- if val == 0 and idx >= fcntl_arg :
161+ if val == 0 and idx >= arg :
161162 ql .os .fd [idx ] = new_fd
162163 regreturn = idx
163164 break
164165 else :
165166 regreturn = - 1
166167 else :
167168 regreturn = - 1
168- elif fcntl_cmd == F_GETFL :
169+
170+ elif cmd == F_GETFL :
169171 regreturn = 2
170- elif fcntl_cmd == F_SETFL :
171- if isinstance (ql .os .fd [fcntl_fd ], ql_socket ):
172- ql .os .fd [fcntl_fd ].fcntl (fcntl_cmd , fcntl_arg )
172+
173+ elif cmd == F_SETFL :
174+ if isinstance (ql .os .fd [fd ], ql_socket ):
175+ ql .os .fd [fd ].fcntl (cmd , arg )
173176 regreturn = 0
174- elif fcntl_cmd == F_GETFD :
177+
178+ elif cmd == F_GETFD :
175179 regreturn = 2
176- elif fcntl_cmd == F_SETFD :
180+
181+ elif cmd == F_SETFD :
177182 regreturn = 0
183+
178184 else :
179185 regreturn = 0
180186
181187 return regreturn
182188
183189
184- def ql_syscall_flock (ql , flock_fd , flock_operation , * args , ** kw ):
190+ def ql_syscall_flock (ql : Qiling , fd : int , operation : int ):
185191 # Should always return 0, we don't need a actual file lock
186- regreturn = 0
187- return regreturn
188192
193+ return 0
189194
190- def ql_syscall_rename (ql : Qiling , oldname_buf , newname_buf , * args , ** kw ):
195+
196+ def ql_syscall_rename (ql : Qiling , oldname_buf : int , newname_buf : int ):
191197 """
192198 rename(const char *oldpath, const char *newpath)
193199 description: change the name or location of a file
@@ -208,10 +214,8 @@ def ql_syscall_rename(ql: Qiling, oldname_buf, newname_buf, *args, **kw):
208214
209215 try :
210216 os .rename (old_realpath , new_realpath )
211- regreturn = 0
212217 except OSError :
213- ql .log .exception (f"rename(): { newpath } is exist !" )
218+ ql .log .exception (f"rename(): { newpath } exists !" )
214219 regreturn = - 1
215220
216221 return regreturn
217-
0 commit comments