c++ - Logically extending file fails -


the following code fails when trying logically extend windows 8.1 file setfilevailddata().

the returned windows error code , message is:

error_privilege_not_held 1314 (0x522) required privilege not held client.

i'm running code administrator , have asserted process indeed has se_manage_volume_name privilege using openprocesstoken() , gettokeninformation().

// setfilevaliddata_test.cpp : defines entry point console application. //  #include "stdafx.h" #include <iostream> #include <windows.h>  bool processhassemanagevolumeprivilege();  int _tmain(int argc, _tchar* argv[]) {     // set access methods     dword accessmethods = generic_read | generic_write;      // set share modes     dword sharemodes = 0;      // set security attributes     lpsecurity_attributes secattr = null;      // set creation disposition     dword creationdispositions = create_always;      // set file flags     dword fileflags = 0;      // set template     handle templatefile = null;      if (!processhassemanagevolumeprivilege())     {         // missing privilege continue         std::cerr << "process missing required se_manage_volume_name (\"semanagevolumeprivilege\") privilege." << std::endl;         return -1;     }      // create file     handle filehandle = createfile(         l"testfile.tmp",         accessmethods,         sharemodes,         secattr,         creationdispositions,         fileflags,         templatefile);     if (filehandle == invalid_handle_value)     {         // error         std::cerr << "createfile() failed error #" << getlasterror() << "." << std::endl;         return -1;     }      // extend file 1 mb     if (!setfilevaliddata(filehandle, 1024*1024))     {         // error         std::cerr << "setfilevaliddata() failed error #" << getlasterror() << "." << std::endl;         return -1;     }      std::cout << "file logically extended successfully!" << std::endl          << "press key quit..." << std::endl;     getchar();      return 0; }  bool processhassemanagevolumeprivilege() {     handle token;     void* tpv;     token_privileges* tp;     dword rl;     bool hasprivilege = false;      std::cout << "asserting process has \"semanagevolumeprivilege\" privilege:" << std::endl;      if (!openprocesstoken(getcurrentprocess(), token_all_access, &token))     {         std::cerr << "openprocesstoken() failed error #" << getlasterror() << "." << std::endl;         return false;     }      if (!gettokeninformation(token, tokenprivileges, null, 0, &rl))     {         dword dw = getlasterror();         if (dw != error_insufficient_buffer)         {             std::cerr << "gettokeninformation() failed error #" << getlasterror() << "." << std::endl;             return false;         }     }      tpv = malloc(rl + 10);     if (!tpv) throw std::bad_alloc();      zeromemory(tpv, rl + 10);      if (!gettokeninformation(token, tokenprivileges, tpv, rl, &rl))     {         std::cerr << "gettokeninformation() failed error #" << getlasterror() << "." << std::endl;         return false;     }       tp = (token_privileges*)tpv;     (dword = 0; < tp->privilegecount; i++)     {         const int nlen = 100;         tchar bufname[nlen];         dword bufsize = nlen;          try         {             lookupprivilegename(null, &tp->privileges[i].luid, bufname, &bufsize);             bufname[nlen - 1] = '\0';             std::wcout << "\t" << bufname;             if (wcscmp(l"semanagevolumeprivilege", bufname) == 0)             {                 std::cout << " ... yes! found it!" << std::endl;                 hasprivilege = true;                 break;             }             else std::cout << " ... no" << std::endl;         }         catch (...)         {             // clean before re-throwing exception                     free(tpv);             closehandle(token);             throw;         }     }      free(tpv);     closehandle(token);     token = null;      return hasprivilege; } 

result:

c:\dev\setfilevaliddata_test\debug> setfilevaliddata_test.exe asserting process has "semanagevolumeprivilege" privilege:         seincreasequotaprivilege ... no         sesecurityprivilege ... no         setakeownershipprivilege ... no         seloaddriverprivilege ... no         sesystemprofileprivilege ... no         sesystemtimeprivilege ... no         seprofilesingleprocessprivilege ... no         seincreasebasepriorityprivilege ... no         secreatepagefileprivilege ... no         sebackupprivilege ... no         serestoreprivilege ... no         seshutdownprivilege ... no         sedebugprivilege ... no         sesystemenvironmentprivilege ... no         sechangenotifyprivilege ... no         seremoteshutdownprivilege ... no         seundockprivilege ... no         semanagevolumeprivilege ... yes! found it! setfilevaliddata() failed error #1314.  c:\dev\setfilevaliddata_test\debug> 

privileges need enabled before can use them. here's code 1 of projects enables se_increase_quote_name , se_assignprimarytoken_name, can modified enable other privileges.

bool enable_privs(void) {     handle token;      struct {         dword count;         luid_and_attributes privilege[2];     } token_privileges;      token_privileges.count = 2;     token_privileges.privilege[0].attributes = se_privilege_enabled;     token_privileges.privilege[1].attributes = se_privilege_enabled;      if (!lookupprivilegevalue(0, se_increase_quota_name, &token_privileges.privilege[0].luid)) return false;     if (!lookupprivilegevalue(0, se_assignprimarytoken_name, &token_privileges.privilege[1].luid)) return false;     if (!openprocesstoken(getcurrentprocess(), token_adjust_privileges, &token)) return false;     if (!adjusttokenprivileges(token, 0, (ptoken_privileges)&token_privileges, 0, 0, 0)) return false;     if (getlasterror() != error_success) return false;      return true; } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -