Skip to content

Commit ec14e81

Browse files
committed
Identify the user group and add the appropriate prefix to the names.
1 parent bbd063f commit ec14e81

File tree

7 files changed

+66
-3
lines changed

7 files changed

+66
-3
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#pragma once
2+
3+
#include <Windows.h>
4+
5+
#include "libipc/memory/resource.h"
6+
7+
namespace ipc {
8+
namespace detail {
9+
10+
/// \brief This routine returns `true` if the caller's process is a member of the Administrators local group.
11+
/// Caller is NOT expected to be impersonating anyone and is expected to be able to open its own process and process token.
12+
/// \return true - Caller has Administrators local group.
13+
/// false - Caller does not have Administrators local group.
14+
/// \see https://learn.microsoft.com/en-us/windows/win32/api/securitybaseapi/nf-securitybaseapi-checktokenmembership
15+
inline bool is_user_admin() {
16+
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
17+
PSID AdministratorsGroup;
18+
BOOL b = AllocateAndInitializeSid(&NtAuthority,
19+
2,
20+
SECURITY_BUILTIN_DOMAIN_RID,
21+
DOMAIN_ALIAS_RID_ADMINS,
22+
0, 0, 0, 0, 0, 0,
23+
&AdministratorsGroup);
24+
if (b) {
25+
if (!CheckTokenMembership(NULL, AdministratorsGroup, &b)) {
26+
b = FALSE;
27+
}
28+
FreeSid(AdministratorsGroup);
29+
}
30+
return !!(b);
31+
}
32+
33+
/// \brief Identify the user group and add the appropriate prefix to the string.
34+
/// \see http://msdn.microsoft.com/en-us/library/aa366551(v=VS.85).aspx
35+
/// https://stackoverflow.com/questions/3999157/system-error-0x5-createfilemapping
36+
inline ipc::string make_comfortable_prefix(ipc::string &&txt) {
37+
if (is_user_admin()) {
38+
return ipc::string{"Global\\"} + txt;
39+
}
40+
return txt;
41+
}
42+
43+
} // namespace detail
44+
} // namespace ipc

src/libipc/platform/win/mutex.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "to_tchar.h"
1111
#include "get_sa.h"
12+
#include "comfortable_prefix.h"
1213

1314
namespace ipc {
1415
namespace detail {
@@ -33,7 +34,8 @@ class mutex {
3334

3435
bool open(char const *name) noexcept {
3536
close();
36-
h_ = ::CreateMutex(detail::get_sa(), FALSE, ipc::detail::to_tchar(ipc::string{"Global\\"} + name).c_str());
37+
h_ = ::CreateMutex(detail::get_sa(), FALSE,
38+
detail::to_tchar(detail::make_comfortable_prefix(name)).c_str());
3739
if (h_ == NULL) {
3840
ipc::error("fail CreateMutex[%lu]: %s\n", ::GetLastError(), name);
3941
return false;

src/libipc/platform/win/semaphore.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "to_tchar.h"
1010
#include "get_sa.h"
11+
#include "comfortable_prefix.h"
1112

1213
namespace ipc {
1314
namespace detail {
@@ -32,7 +33,7 @@ class semaphore {
3233
close();
3334
h_ = ::CreateSemaphore(detail::get_sa(),
3435
static_cast<LONG>(count), LONG_MAX,
35-
ipc::detail::to_tchar(ipc::string{"Global\\"} + name).c_str());
36+
detail::to_tchar(detail::make_comfortable_prefix(name)).c_str());
3637
if (h_ == NULL) {
3738
ipc::error("fail CreateSemaphore[%lu]: %s\n", ::GetLastError(), name);
3839
return false;

src/libipc/platform/win/shm_win.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "to_tchar.h"
1515
#include "get_sa.h"
16+
#include "comfortable_prefix.h"
1617

1718
namespace {
1819

@@ -33,7 +34,7 @@ id_t acquire(char const * name, std::size_t size, unsigned mode) {
3334
return nullptr;
3435
}
3536
HANDLE h;
36-
auto fmt_name = ipc::detail::to_tchar(ipc::string{"Global\\__IPC_SHM__"} + name);
37+
auto fmt_name = ipc::detail::to_tchar(detail::make_comfortable_prefix("__IPC_SHM__") + name);
3738
// Opens a named file mapping object.
3839
if (mode == open) {
3940
h = ::OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, fmt_name.c_str());

src/libipc/sync/condition.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "libipc/condition.h"
33

44
#include "libipc/utility/pimpl.h"
5+
#include "libipc/utility/log.h"
56
#include "libipc/memory/resource.h"
67
#include "libipc/platform/detail.h"
78
#if defined(IPC_OS_WINDOWS_)
@@ -49,6 +50,10 @@ bool condition::valid() const noexcept {
4950
}
5051

5152
bool condition::open(char const *name) noexcept {
53+
if (name == nullptr || name[0] == '\0') {
54+
ipc::error("fail condition open: name is empty\n");
55+
return false;
56+
}
5257
return impl(p_)->cond_.open(name);
5358
}
5459

src/libipc/sync/mutex.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "libipc/mutex.h"
33

44
#include "libipc/utility/pimpl.h"
5+
#include "libipc/utility/log.h"
56
#include "libipc/memory/resource.h"
67
#include "libipc/platform/detail.h"
78
#if defined(IPC_OS_WINDOWS_)
@@ -49,6 +50,10 @@ bool mutex::valid() const noexcept {
4950
}
5051

5152
bool mutex::open(char const *name) noexcept {
53+
if (name == nullptr || name[0] == '\0') {
54+
ipc::error("fail mutex open: name is empty\n");
55+
return false;
56+
}
5257
return impl(p_)->lock_.open(name);
5358
}
5459

src/libipc/sync/semaphore.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "libipc/semaphore.h"
33

44
#include "libipc/utility/pimpl.h"
5+
#include "libipc/utility/log.h"
56
#include "libipc/memory/resource.h"
67
#include "libipc/platform/detail.h"
78
#if defined(IPC_OS_WINDOWS_)
@@ -47,6 +48,10 @@ bool semaphore::valid() const noexcept {
4748
}
4849

4950
bool semaphore::open(char const *name, std::uint32_t count) noexcept {
51+
if (name == nullptr || name[0] == '\0') {
52+
ipc::error("fail semaphore open: name is empty\n");
53+
return false;
54+
}
5055
return impl(p_)->sem_.open(name, count);
5156
}
5257

0 commit comments

Comments
 (0)