comparison common/util.c @ 1029:6684e5012b7a

(issue98) Set integrity level to medium on restricted token and evaluate it to determine if the process is elevated.
author Andre Heinecke <andre.heinecke@intevation.de>
date Thu, 04 Sep 2014 11:00:55 +0200
parents 427e2e18b8c8
children 1f23803e1f83
comparison
equal deleted inserted replaced
1028:461db8f903f5 1029:6684e5012b7a
404 return retval; 404 return retval;
405 } 405 }
406 #endif 406 #endif
407 407
408 bool 408 bool
409 has_high_integrity(HANDLE hToken)
410 {
411 PTOKEN_MANDATORY_LABEL integrity_label = NULL;
412 DWORD integrity_level = 0,
413 size = 0;
414
415 if (hToken == NULL || hToken == INVALID_HANDLE_VALUE)
416 {
417 DEBUGPRINTF ("Invalid parameters.");
418 return false;
419 }
420
421 /* Get the required size */
422 if (!GetTokenInformation(hToken, TokenIntegrityLevel,
423 NULL, 0, &size) == ERROR_INSUFFICIENT_BUFFER)
424 {
425 PRINTLASTERROR ("Failed to get required size.\n");
426 return false;
427 }
428 integrity_label = (PTOKEN_MANDATORY_LABEL) LocalAlloc(0, size);
429 if (integrity_label == NULL)
430 {
431 ERRORPRINTF ("Failed to allocate label. \n");
432 return false;
433 }
434
435 if (!GetTokenInformation(hToken, TokenIntegrityLevel,
436 integrity_label, size, &size))
437 {
438 PRINTLASTERROR ("Failed to get integrity level.\n");
439 LocalFree(integrity_label);
440 return false;
441 }
442
443 /* Get the last integrity level */
444 integrity_level = *GetSidSubAuthority(integrity_label->Label.Sid,
445 (DWORD)(UCHAR)(*GetSidSubAuthorityCount(
446 integrity_label->Label.Sid) - 1));
447
448 LocalFree (integrity_label);
449
450 return integrity_level >= SECURITY_MANDATORY_HIGH_RID;
451 }
452
453 bool
409 is_elevated() 454 is_elevated()
410 { 455 {
411 bool ret = false; 456 bool ret = false;
412 #ifndef _WIN32 457 #ifndef _WIN32
413 ret = (geteuid() == 0); 458 ret = (geteuid() == 0);
421 sizeof (TokenElevation), &cbSize)) 466 sizeof (TokenElevation), &cbSize))
422 { 467 {
423 ret = elevation; 468 ret = elevation;
424 } 469 }
425 } 470 }
471 /* Elevation will be true and ElevationType TokenElevationTypeFull even
472 if the token is a user token created by SAFER so we additionally
473 check the integrity level of the token which will only be high in
474 the real elevated process and medium otherwise. */
475
476 ret = ret && has_high_integrity (hToken);
477
426 if (hToken) 478 if (hToken)
427 CloseHandle (hToken); 479 CloseHandle (hToken);
428 #endif 480 #endif
429 return ret; 481 return ret;
430 } 482 }

http://wald.intevation.org/projects/trustbridge/