@@ -1724,17 +1724,6 @@ pgwin32_doRunAsService(void)
1724
1724
}
1725
1725
1726
1726
1727
- /*
1728
- * Mingw headers are incomplete, and so are the libraries. So we have to load
1729
- * a whole lot of API functions dynamically.
1730
- */
1731
- typedef BOOL (WINAPI * __CreateRestrictedToken ) (HANDLE , DWORD , DWORD , PSID_AND_ATTRIBUTES , DWORD , PLUID_AND_ATTRIBUTES , DWORD , PSID_AND_ATTRIBUTES , PHANDLE );
1732
- typedef BOOL (WINAPI * __IsProcessInJob ) (HANDLE , HANDLE , PBOOL );
1733
- typedef HANDLE (WINAPI * __CreateJobObject ) (LPSECURITY_ATTRIBUTES , LPCTSTR );
1734
- typedef BOOL (WINAPI * __SetInformationJobObject ) (HANDLE , JOBOBJECTINFOCLASS , LPVOID , DWORD );
1735
- typedef BOOL (WINAPI * __AssignProcessToJobObject ) (HANDLE , HANDLE );
1736
- typedef BOOL (WINAPI * __QueryInformationJobObject ) (HANDLE , JOBOBJECTINFOCLASS , LPVOID , DWORD , LPDWORD );
1737
-
1738
1727
/*
1739
1728
* Set up STARTUPINFO for the new process to inherit this process' handles.
1740
1729
*
@@ -1777,20 +1766,11 @@ CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION *processInfo, bool as_ser
1777
1766
STARTUPINFO si ;
1778
1767
HANDLE origToken ;
1779
1768
HANDLE restrictedToken ;
1769
+ BOOL inJob ;
1780
1770
SID_IDENTIFIER_AUTHORITY NtAuthority = {SECURITY_NT_AUTHORITY };
1781
1771
SID_AND_ATTRIBUTES dropSids [2 ];
1782
1772
PTOKEN_PRIVILEGES delPrivs ;
1783
1773
1784
- /* Functions loaded dynamically */
1785
- __CreateRestrictedToken _CreateRestrictedToken = NULL ;
1786
- __IsProcessInJob _IsProcessInJob = NULL ;
1787
- __CreateJobObject _CreateJobObject = NULL ;
1788
- __SetInformationJobObject _SetInformationJobObject = NULL ;
1789
- __AssignProcessToJobObject _AssignProcessToJobObject = NULL ;
1790
- __QueryInformationJobObject _QueryInformationJobObject = NULL ;
1791
- HANDLE Kernel32Handle ;
1792
- HANDLE Advapi32Handle ;
1793
-
1794
1774
ZeroMemory (& si , sizeof (si ));
1795
1775
si .cb = sizeof (si );
1796
1776
@@ -1802,20 +1782,6 @@ CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION *processInfo, bool as_ser
1802
1782
*/
1803
1783
InheritStdHandles (& si );
1804
1784
1805
- Advapi32Handle = LoadLibrary ("ADVAPI32.DLL" );
1806
- if (Advapi32Handle != NULL )
1807
- {
1808
- _CreateRestrictedToken = (__CreateRestrictedToken ) (pg_funcptr_t ) GetProcAddress (Advapi32Handle , "CreateRestrictedToken" );
1809
- }
1810
-
1811
- if (_CreateRestrictedToken == NULL )
1812
- {
1813
- /* Log error if we cannot get the function */
1814
- write_stderr (_ ("%s: could not locate object function to create restricted token: error code %lu\n" ),
1815
- progname , (unsigned long ) GetLastError ());
1816
- return 0 ;
1817
- }
1818
-
1819
1785
/* Open the current token to use as a base for the restricted one */
1820
1786
if (!OpenProcessToken (GetCurrentProcess (), TOKEN_ALL_ACCESS , & origToken ))
1821
1787
{
@@ -1848,19 +1814,18 @@ CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION *processInfo, bool as_ser
1848
1814
/* Error message already printed */
1849
1815
return 0 ;
1850
1816
1851
- b = _CreateRestrictedToken (origToken ,
1852
- 0 ,
1853
- sizeof (dropSids ) / sizeof (dropSids [0 ]),
1854
- dropSids ,
1855
- delPrivs -> PrivilegeCount , delPrivs -> Privileges ,
1856
- 0 , NULL ,
1857
- & restrictedToken );
1817
+ b = CreateRestrictedToken (origToken ,
1818
+ 0 ,
1819
+ sizeof (dropSids ) / sizeof (dropSids [0 ]),
1820
+ dropSids ,
1821
+ delPrivs -> PrivilegeCount , delPrivs -> Privileges ,
1822
+ 0 , NULL ,
1823
+ & restrictedToken );
1858
1824
1859
1825
free (delPrivs );
1860
1826
FreeSid (dropSids [1 ].Sid );
1861
1827
FreeSid (dropSids [0 ].Sid );
1862
1828
CloseHandle (origToken );
1863
- FreeLibrary (Advapi32Handle );
1864
1829
1865
1830
if (!b )
1866
1831
{
@@ -1872,79 +1837,55 @@ CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION *processInfo, bool as_ser
1872
1837
AddUserToTokenDacl (restrictedToken );
1873
1838
r = CreateProcessAsUser (restrictedToken , NULL , cmd , NULL , NULL , TRUE, CREATE_SUSPENDED , NULL , NULL , & si , processInfo );
1874
1839
1875
- Kernel32Handle = LoadLibrary ("KERNEL32.DLL" );
1876
- if (Kernel32Handle != NULL )
1877
- {
1878
- _IsProcessInJob = (__IsProcessInJob ) (pg_funcptr_t ) GetProcAddress (Kernel32Handle , "IsProcessInJob" );
1879
- _CreateJobObject = (__CreateJobObject ) (pg_funcptr_t ) GetProcAddress (Kernel32Handle , "CreateJobObjectA" );
1880
- _SetInformationJobObject = (__SetInformationJobObject ) (pg_funcptr_t ) GetProcAddress (Kernel32Handle , "SetInformationJobObject" );
1881
- _AssignProcessToJobObject = (__AssignProcessToJobObject ) (pg_funcptr_t ) GetProcAddress (Kernel32Handle , "AssignProcessToJobObject" );
1882
- _QueryInformationJobObject = (__QueryInformationJobObject ) (pg_funcptr_t ) GetProcAddress (Kernel32Handle , "QueryInformationJobObject" );
1883
- }
1884
-
1885
- /* Verify that we found all functions */
1886
- if (_IsProcessInJob == NULL || _CreateJobObject == NULL || _SetInformationJobObject == NULL || _AssignProcessToJobObject == NULL || _QueryInformationJobObject == NULL )
1840
+ if (IsProcessInJob (processInfo -> hProcess , NULL , & inJob ))
1887
1841
{
1888
- /* Log error if we can't get version */
1889
- write_stderr (_ ("%s: WARNING: could not locate all job object functions in system API\n" ), progname );
1890
- }
1891
- else
1892
- {
1893
- BOOL inJob ;
1894
-
1895
- if (_IsProcessInJob (processInfo -> hProcess , NULL , & inJob ))
1842
+ if (!inJob )
1896
1843
{
1897
- if (!inJob )
1898
- {
1899
- /*
1900
- * Job objects are working, and the new process isn't in one,
1901
- * so we can create one safely. If any problems show up when
1902
- * setting it, we're going to ignore them.
1903
- */
1904
- HANDLE job ;
1905
- char jobname [128 ];
1844
+ /*
1845
+ * Job objects are working, and the new process isn't in one, so
1846
+ * we can create one safely. If any problems show up when setting
1847
+ * it, we're going to ignore them.
1848
+ */
1849
+ HANDLE job ;
1850
+ char jobname [128 ];
1906
1851
1907
- sprintf (jobname , "PostgreSQL_%lu" ,
1908
- (unsigned long ) processInfo -> dwProcessId );
1852
+ sprintf (jobname , "PostgreSQL_%lu" ,
1853
+ (unsigned long ) processInfo -> dwProcessId );
1909
1854
1910
- job = _CreateJobObject (NULL , jobname );
1911
- if (job )
1912
- {
1913
- JOBOBJECT_BASIC_LIMIT_INFORMATION basicLimit ;
1914
- JOBOBJECT_BASIC_UI_RESTRICTIONS uiRestrictions ;
1915
- JOBOBJECT_SECURITY_LIMIT_INFORMATION securityLimit ;
1855
+ job = CreateJobObject (NULL , jobname );
1856
+ if (job )
1857
+ {
1858
+ JOBOBJECT_BASIC_LIMIT_INFORMATION basicLimit ;
1859
+ JOBOBJECT_BASIC_UI_RESTRICTIONS uiRestrictions ;
1860
+ JOBOBJECT_SECURITY_LIMIT_INFORMATION securityLimit ;
1916
1861
1917
- ZeroMemory (& basicLimit , sizeof (basicLimit ));
1918
- ZeroMemory (& uiRestrictions , sizeof (uiRestrictions ));
1919
- ZeroMemory (& securityLimit , sizeof (securityLimit ));
1862
+ ZeroMemory (& basicLimit , sizeof (basicLimit ));
1863
+ ZeroMemory (& uiRestrictions , sizeof (uiRestrictions ));
1864
+ ZeroMemory (& securityLimit , sizeof (securityLimit ));
1920
1865
1921
- basicLimit .LimitFlags = JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION | JOB_OBJECT_LIMIT_PRIORITY_CLASS ;
1922
- basicLimit .PriorityClass = NORMAL_PRIORITY_CLASS ;
1923
- _SetInformationJobObject (job , JobObjectBasicLimitInformation , & basicLimit , sizeof (basicLimit ));
1866
+ basicLimit .LimitFlags = JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION | JOB_OBJECT_LIMIT_PRIORITY_CLASS ;
1867
+ basicLimit .PriorityClass = NORMAL_PRIORITY_CLASS ;
1868
+ SetInformationJobObject (job , JobObjectBasicLimitInformation , & basicLimit , sizeof (basicLimit ));
1924
1869
1925
- uiRestrictions .UIRestrictionsClass = JOB_OBJECT_UILIMIT_DESKTOP | JOB_OBJECT_UILIMIT_DISPLAYSETTINGS |
1926
- JOB_OBJECT_UILIMIT_EXITWINDOWS | JOB_OBJECT_UILIMIT_READCLIPBOARD |
1927
- JOB_OBJECT_UILIMIT_SYSTEMPARAMETERS | JOB_OBJECT_UILIMIT_WRITECLIPBOARD ;
1870
+ uiRestrictions .UIRestrictionsClass = JOB_OBJECT_UILIMIT_DESKTOP | JOB_OBJECT_UILIMIT_DISPLAYSETTINGS |
1871
+ JOB_OBJECT_UILIMIT_EXITWINDOWS | JOB_OBJECT_UILIMIT_READCLIPBOARD |
1872
+ JOB_OBJECT_UILIMIT_SYSTEMPARAMETERS | JOB_OBJECT_UILIMIT_WRITECLIPBOARD ;
1928
1873
1929
- _SetInformationJobObject (job , JobObjectBasicUIRestrictions , & uiRestrictions , sizeof (uiRestrictions ));
1874
+ SetInformationJobObject (job , JobObjectBasicUIRestrictions , & uiRestrictions , sizeof (uiRestrictions ));
1930
1875
1931
- securityLimit .SecurityLimitFlags = JOB_OBJECT_SECURITY_NO_ADMIN | JOB_OBJECT_SECURITY_ONLY_TOKEN ;
1932
- securityLimit .JobToken = restrictedToken ;
1933
- _SetInformationJobObject (job , JobObjectSecurityLimitInformation , & securityLimit , sizeof (securityLimit ));
1876
+ securityLimit .SecurityLimitFlags = JOB_OBJECT_SECURITY_NO_ADMIN | JOB_OBJECT_SECURITY_ONLY_TOKEN ;
1877
+ securityLimit .JobToken = restrictedToken ;
1878
+ SetInformationJobObject (job , JobObjectSecurityLimitInformation , & securityLimit , sizeof (securityLimit ));
1934
1879
1935
- _AssignProcessToJobObject (job , processInfo -> hProcess );
1936
- }
1880
+ AssignProcessToJobObject (job , processInfo -> hProcess );
1937
1881
}
1938
1882
}
1939
1883
}
1940
1884
1941
-
1942
1885
CloseHandle (restrictedToken );
1943
1886
1944
1887
ResumeThread (processInfo -> hThread );
1945
1888
1946
- FreeLibrary (Kernel32Handle );
1947
-
1948
1889
/*
1949
1890
* We intentionally don't close the job object handle, because we want the
1950
1891
* object to live on until pg_ctl shuts down.
0 commit comments