Skip to content

Commit 3f03eb4

Browse files
committed
Minor robustness fixes to POSIX syscalls
1 parent 913a79a commit 3f03eb4

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

qiling/os/posix/syscall/unistd.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from multiprocessing import Process
1313

1414
from qiling import Qiling
15-
from qiling.const import QL_ARCH, QL_OS, QL_VERBOSE
15+
from qiling.const import QL_ARCH, QL_OS
1616
from qiling.os.posix.filestruct import ql_pipe
1717
from qiling.os.posix.const import *
1818
from qiling.os.posix.stat import Stat
@@ -159,12 +159,20 @@ def ql_syscall_lseek(ql: Qiling, fd: int, offset: int, origin: int):
159159

160160

161161
def ql_syscall__llseek(ql: Qiling, fd: int, offset_high: int, offset_low: int, result: int, whence: int):
162+
if fd not in range(NR_OPEN):
163+
return -EBADF
164+
165+
f = ql.os.fd[fd]
166+
167+
if f is None:
168+
return -EBADF
169+
162170
# treat offset as a signed value
163171
offset = ql.unpack64s(ql.pack64((offset_high << 32) | offset_low))
164172
origin = whence
165173

166174
try:
167-
ret = ql.os.fd[fd].seek(offset, origin)
175+
ret = f.seek(offset, origin)
168176
except OSError:
169177
regreturn = -1
170178
else:
@@ -278,19 +286,29 @@ def ql_syscall_read(ql: Qiling, fd, buf: int, length: int):
278286

279287

280288
def ql_syscall_write(ql: Qiling, fd: int, buf: int, count: int):
289+
if fd not in range(NR_OPEN):
290+
return -EBADF
291+
292+
f = ql.os.fd[fd]
293+
294+
if f is None:
295+
return -EBADF
296+
281297
try:
282298
data = ql.mem.read(buf, count)
283299
except:
284300
regreturn = -1
285301
else:
286302
ql.log.debug(f'write() CONTENT: {bytes(data)}')
287303

288-
if hasattr(ql.os.fd[fd], 'write'):
289-
ql.os.fd[fd].write(data)
304+
if hasattr(f, 'write'):
305+
f.write(data)
306+
307+
regreturn = count
290308
else:
291309
ql.log.warning(f'write failed since fd {fd:d} does not have a write method')
310+
regreturn = -1
292311

293-
regreturn = count
294312

295313
return regreturn
296314

0 commit comments

Comments
 (0)