Skip to content

Commit a3bccf5

Browse files
committed
Fix buffer overrun issue in LCMapString implementation
1 parent 6adafd5 commit a3bccf5

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

qiling/os/windows/dlls/kernel32/winnls.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,19 @@ def hook_IsValidCodePage(ql: Qiling, address: int, params):
8080
return 1
8181

8282
def __LCMapString(ql: Qiling, address: int, params, wstring: bool):
83-
lpSrcStr: str = params["lpSrcStr"]
83+
lpSrcStr: int = params["lpSrcStr"]
84+
cchSrc: int = params["cchSrc"]
8485
lpDestStr: int = params["lpDestStr"]
8586
cchDest: int = params["cchDest"]
8687

87-
enc = "utf-16le" if wstring else "utf-8"
88-
res = f'{lpSrcStr}\x00'
88+
char_size = 2 if wstring else 1
89+
byte_count = cchSrc * char_size
8990

9091
if cchDest and lpDestStr:
91-
# TODO maybe do some other check, for now is working
92-
ql.mem.write(lpDestStr, res.encode(enc))
92+
source_bytes = ql.mem.read(lpSrcStr, byte_count)
93+
ql.mem.write(lpDestStr, bytes(source_bytes))
9394

94-
return len(res)
95+
return cchSrc
9596

9697
# int LCMapStringW(
9798
# LCID Locale,
@@ -104,9 +105,9 @@ def __LCMapString(ql: Qiling, address: int, params, wstring: bool):
104105
@winsdkapi(cc=STDCALL, params={
105106
'Locale' : LCID,
106107
'dwMapFlags' : DWORD,
107-
'lpSrcStr' : LPCWSTR,
108+
'lpSrcStr' : POINTER,
108109
'cchSrc' : INT,
109-
'lpDestStr' : LPWSTR,
110+
'lpDestStr' : POINTER,
110111
'cchDest' : INT
111112
})
112113
def hook_LCMapStringW(ql: Qiling, address: int, params):
@@ -123,9 +124,9 @@ def hook_LCMapStringW(ql: Qiling, address: int, params):
123124
@winsdkapi(cc=STDCALL, params={
124125
'Locale' : LCID,
125126
'dwMapFlags' : DWORD,
126-
'lpSrcStr' : LPCSTR,
127+
'lpSrcStr' : POINTER,
127128
'cchSrc' : INT,
128-
'lpDestStr' : LPSTR,
129+
'lpDestStr' : POINTER,
129130
'cchDest' : INT
130131
})
131132
def hook_LCMapStringA(ql: Qiling, address: int, params):
@@ -145,9 +146,9 @@ def hook_LCMapStringA(ql: Qiling, address: int, params):
145146
@winsdkapi(cc=STDCALL, params={
146147
'lpLocaleName' : LPCWSTR,
147148
'dwMapFlags' : DWORD,
148-
'lpSrcStr' : LPCWSTR,
149+
'lpSrcStr' : POINTER,
149150
'cchSrc' : INT,
150-
'lpDestStr' : LPWSTR,
151+
'lpDestStr' : POINTER,
151152
'cchDest' : INT,
152153
'lpVersionInformation' : LPNLSVERSIONINFO,
153154
'lpReserved' : LPVOID,

0 commit comments

Comments
 (0)