Skip to content

Commit

Permalink
add dummy operation to afterFunc()@fixscancodemap.cpp to avoid side e…
Browse files Browse the repository at this point in the history
…ffect of optimization

put error dialog with error code on fail of changing security info for mainslot and escape NLS Keys
  • Loading branch information
U-i7\gimy committed Aug 14, 2009
1 parent 40f19a8 commit f47d606
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 48 deletions.
51 changes: 29 additions & 22 deletions fixscancodemap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ static DWORD invokeFunc(InjectInfo *info)
info->pCloseHandle(hProcess);
return 0;
}
static void afterFunc(void){}
static int afterFunc(int arg)
{
// dummy operation
// if this function empty, optimizer unify this with other empty functions.
// following code avoid it.
arg *= 710810; // non-sense operation
return arg;
}
#pragma runtime_checks( "", restore )

const DWORD FixScancodeMap::s_fixEntryNum = 4;
Expand All @@ -63,13 +70,13 @@ int FixScancodeMap::acquirePrivileges()
HANDLE hToken = NULL;

if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken)) {
ret = 1;
ret = 5;
goto exit;
}

LUID luid;
if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid)) {
ret = 2;
ret = 6;
goto exit;
}

Expand All @@ -79,7 +86,7 @@ int FixScancodeMap::acquirePrivileges()
tk_priv.Privileges[0].Luid = luid;

if (!AdjustTokenPrivileges(hToken, FALSE, &tk_priv, 0, NULL, NULL)) {
ret = 3;
ret = 7;
goto exit;
}

Expand Down Expand Up @@ -142,33 +149,33 @@ int FixScancodeMap::injectThread(DWORD dwPID)
DWORD memSize = afterFuncAddr - invokeFuncAddr;

if ((hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPID)) == NULL) {
ret = 1;
ret = 8;
goto exit;
}

remoteMem = VirtualAllocEx(hProcess, NULL, memSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
if (remoteMem == NULL) {
ret = 2;
ret = 9;
err = GetLastError();
goto exit;
}

wFlag = WriteProcessMemory(hProcess, remoteMem, (char*)invokeFunc, memSize, (SIZE_T*)0);
if (wFlag == FALSE) {
ret = 3;
ret = 10;
goto exit;
}

remoteInfo = VirtualAllocEx(hProcess, NULL, sizeof(m_info), MEM_COMMIT, PAGE_READWRITE);
if (remoteInfo == NULL) {
ret = 2;
ret = 11;
err = GetLastError();
goto exit;
}

wFlag = WriteProcessMemory(hProcess, remoteInfo, (char*)&m_info, sizeof(m_info), (SIZE_T*)0);
if (wFlag == FALSE) {
ret = 3;
ret = 12;
goto exit;
}

Expand All @@ -192,12 +199,12 @@ int FixScancodeMap::injectThread(DWORD dwPID)
HANDLE hThread = CreateRemoteThread(hProcess, NULL, 0,
(LPTHREAD_START_ROUTINE)remoteMem, remoteInfo, 0, NULL);
if (hThread == NULL) {
ret = 4;
ret = 13;
goto exit;
}

if (WaitForSingleObject(hThread, 5000) == WAIT_TIMEOUT) {
ret = 5;
ret = 14;
goto exit;
}
DWORD result = -1;
Expand Down Expand Up @@ -226,23 +233,23 @@ int FixScancodeMap::update()
MINIMIZEDMETRICS mm;
int result = 0;

if (acquirePrivileges()) {
result = 1;
result = acquirePrivileges();
if (result) {
goto exit;
}

DWORD dwPID;
if ((dwPID = getWinLogonPid()) == 0) {
result = 1;
result = 15;
goto exit;
}

memset(&mm, 0, sizeof(mm));
mm.cbSize = sizeof(mm);
SystemParametersInfo(SPI_GETMINIMIZEDMETRICS, sizeof(mm), &mm, 0);

if (injectThread(dwPID)) {
result = 1;
result = injectThread(dwPID);
if (result) {
goto exit;
}

Expand All @@ -267,20 +274,20 @@ int FixScancodeMap::fix()
if (ret) {
origMap = reinterpret_cast<ScancodeMap*>(malloc(origSize));
if (origMap == NULL) {
result = 1;
result = 16;
goto exit;
}

ret = reg.read(_T("Scancode Map"), reinterpret_cast<BYTE*>(origMap), &origSize, NULL, 0);
if (ret == false) {
result = 1;
result = 17;
goto exit;
}

fixSize = origSize;
fixMap = reinterpret_cast<ScancodeMap*>(malloc(origSize + s_fixEntryNum * sizeof(s_fixEntry[0])));
if (fixMap == NULL) {
result = 1;
result = 18;
goto exit;
}

Expand All @@ -292,7 +299,7 @@ int FixScancodeMap::fix()
fixSize = sizeof(ScancodeMap);
fixMap = reinterpret_cast<ScancodeMap*>(malloc(sizeof(ScancodeMap) + s_fixEntryNum * sizeof(s_fixEntry[0])));
if (fixMap == NULL) {
result = 1;
result = 19;
goto exit;
}

Expand Down Expand Up @@ -327,7 +334,7 @@ int FixScancodeMap::fix()

ret = reg.write(_T("Scancode Map"), reinterpret_cast<BYTE*>(fixMap), fixSize);
if (ret == false) {
result = 1;
result = 20;
goto exit;
}

Expand All @@ -339,7 +346,7 @@ int FixScancodeMap::fix()
ret = reg.remove(_T("Scancode Map"));
}
if (ret == false) {
result = 1;
result = 21;
goto exit;
}

Expand Down
79 changes: 53 additions & 26 deletions mayu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,11 @@ class Mayu
This = reinterpret_cast<Mayu *>(
reinterpret_cast<CREATESTRUCT *>(i_lParam)->lpCreateParams);
if (This->m_escapeNlsKeys) {
This->m_fixScancodeMap.fix();
int err;
err = This->m_fixScancodeMap.fix();
if (err) {
This->errorDialogWithCode(IDS_escapeNlsKeysFailed, err);
}
}
#ifdef MAYU64
SetWindowLongPtr(i_hwnd, 0, (LONG_PTR)This);
Expand Down Expand Up @@ -776,7 +780,18 @@ class Mayu
}
}

bool enableToWriteByUser(HANDLE hdl)
bool errorDialogWithCode(UINT ids, int code)
{
_TCHAR title[1024];
_TCHAR text[1024];

_sntprintf_s(title, NUMBER_OF(title), _TRUNCATE, loadString(IDS_mayu).c_str());
_sntprintf_s(text, NUMBER_OF(text), _TRUNCATE, loadString(ids).c_str(), code);
MessageBox((HWND)NULL, text, title, MB_OK | MB_ICONSTOP);
return true;
}

int enableToWriteByUser(HANDLE hdl)
{
TCHAR userName[GANA_MAX_ATOM_LENGTH];
DWORD userNameSize = NUMBER_OF(userName);
Expand All @@ -798,46 +813,49 @@ class Mayu
DWORD newAceIndex = 0;

BOOL ret;
int err = 0;

ret = GetUserName(userName, &userNameSize);
if (ret == FALSE) {
return false;
err = 1;
goto exit;
}

// get buffer size for pSid (and pDomain)
ret = LookupAccountName(NULL, userName, pSid, &sidSize, pDomain, &domainSize, &sidType);
if (ret != FALSE || GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
// above call should fail by ERROR_INSUFFICIENT_BUFFER
return false;
err = 2;
goto exit;
}

pSid = reinterpret_cast<PSID>(LocalAlloc(LPTR, sidSize));
pDomain = reinterpret_cast<TCHAR*>(LocalAlloc(LPTR, domainSize * sizeof(TCHAR)));
if (pSid == NULL || pDomain == NULL) {
LocalFree(pSid);
LocalFree(pDomain);
return false;
err = 3;
goto exit;
}

// get SID (and Domain) for logoned user
ret = LookupAccountName(NULL, userName, pSid, &sidSize, pDomain, &domainSize, &sidType);
if (ret == FALSE) {
// LookupAccountName() should success in this time
LocalFree(pSid);
LocalFree(pDomain);
return false;
err = 4;
goto exit;
}

// get DACL for hdl
ret = GetSecurityInfo(hdl, SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, NULL, NULL, &pOrigDacl, NULL, &pSd);
if (ret != ERROR_SUCCESS) {
return false;
err = 5;
goto exit;
}

// get size for original DACL
ret = GetAclInformation(pOrigDacl, &aclInfo, sizeof(aclInfo), AclSizeInformation);
if (ret == FALSE) {
return false;
err = 6;
goto exit;
}

// compute size for new DACL
Expand All @@ -846,15 +864,15 @@ class Mayu
// allocate memory for new DACL
pNewDacl = reinterpret_cast<PACL>(LocalAlloc(LPTR, newDaclSize));
if (pNewDacl == NULL) {
LocalFree(pSid);
LocalFree(pDomain);
return false;
err = 7;
goto exit;
}

// initialize new DACL
ret = InitializeAcl(pNewDacl, newDaclSize, ACL_REVISION);
if (ret == FALSE) {
return false;
err = 8;
goto exit;
}

// copy original DACL to new DACL
Expand All @@ -863,7 +881,8 @@ class Mayu

ret = GetAce(pOrigDacl, aceIndex, &pAce);
if (ret == FALSE) {
return false;
err = 9;
goto exit;
}

if ((reinterpret_cast<ACCESS_ALLOWED_ACE*>(pAce))->Header.AceFlags & INHERITED_ACE) {
Expand All @@ -876,15 +895,17 @@ class Mayu

ret = AddAce(pNewDacl, ACL_REVISION, MAXDWORD, pAce, (reinterpret_cast<PACE_HEADER>(pAce))->AceSize);
if (ret == FALSE) {
return false;
err = 10;
goto exit;
}

newAceIndex++;
}

ret = AddAccessAllowedAce(pNewDacl, ACL_REVISION, GENERIC_ALL, pSid);
if (ret == FALSE) {
return false;
err = 11;
goto exit;
}

// copy the rest of inherited ACEs
Expand All @@ -893,27 +914,29 @@ class Mayu

ret = GetAce(pOrigDacl, aceIndex, &pAce);
if (ret == FALSE) {
return false;
err = 12;
goto exit;
}

ret = AddAce(pNewDacl, ACL_REVISION, MAXDWORD, pAce, (reinterpret_cast<PACE_HEADER>(pAce))->AceSize);
if (ret == FALSE) {
return false;
err = 13;
goto exit;
}
}

ret = SetSecurityInfo(hdl, SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, NULL, NULL, pNewDacl, NULL);
if (ret != ERROR_SUCCESS) {
DWORD err = GetLastError();
return false;
err = 14;
}

exit:
LocalFree(pSd);
LocalFree(pSid);
LocalFree(pDomain);
LocalFree(pNewDacl);

return true;
return err;
}

public:
Expand All @@ -936,7 +959,11 @@ class Mayu
#ifdef USE_MAILSLOT
m_hNotifyMailslot = CreateMailslot(NOTIFY_MAILSLOT_NAME, 0, MAILSLOT_WAIT_FOREVER, (SECURITY_ATTRIBUTES *)NULL);
ASSERT(m_hNotifyMailslot != INVALID_HANDLE_VALUE);
enableToWriteByUser(m_hNotifyMailslot);
int err;
err = enableToWriteByUser(m_hNotifyMailslot);
if (err) {
errorDialogWithCode(IDS_cannotPermitStandardUser, err);
}

m_hNotifyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
ASSERT(m_hNotifyEvent);
Expand Down Expand Up @@ -1063,7 +1090,7 @@ class Mayu
title, sizeof(title)/sizeof(title[0]));
_stprintf_s(buf, sizeof(buf)/sizeof(buf[0]),
text, _T("yamyd32"), GetLastError());
MessageBox((HWND)NULL, buf, title, MB_OK | MB_ICONSTOP);
MessageBox((HWND)NULL, buf, title, MB_OK | MB_ICONSTOP);
} else {
CloseHandle(m_pi.hThread);
CloseHandle(m_pi.hProcess);
Expand Down
4 changes: 4 additions & 0 deletions mayu.rc
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ BEGIN
IDS_mayuShellOpen "notepad.exe ""%1"""
IDS_readFromHomeDirectory "(�z�[���f�B���N�g������)"
IDS_cannotInvoke "%s ���N���ł��܂���(0x%08x)"
IDS_cannotPermitStandardUser "�W�����[�U�ւ̋��‚Ɏ��s���܂���(�G���[�R�[�h=%d)"
IDS_escapeNlsKeysFailed "NLS�L�[�̃G�X�P�[�v�Ɏ��s���܂���(�G���[�R�[�h=%d)"
END

STRINGTABLE DISCARDABLE
Expand Down Expand Up @@ -488,6 +490,8 @@ BEGIN
IDS_mayuShellOpen "notepad.exe ""%1"""
IDS_readFromHomeDirectory "(from home directory)"
IDS_cannotInvoke "can't invoke %s(0x%08x)"
IDS_cannotPermitStandardUser "failed to permit standard user(error code=%d)"
IDS_escapeNlsKeysFailed "failed to escape NLS keys(error code=%d)"
END

STRINGTABLE DISCARDABLE
Expand Down
Loading

0 comments on commit f47d606

Please sign in to comment.