Skip to content
This repository was archived by the owner on Dec 27, 2019. It is now read-only.

Commit fb6719f

Browse files
committed
tools: windows: enforce named pipe ownership
1 parent dcca03f commit fb6719f

File tree

2 files changed

+40
-22
lines changed

2 files changed

+40
-22
lines changed

src/tools/ipc.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,17 @@ static int add_next_to_inflatable_buffer(struct inflatable_buffer *buffer)
9696
}
9797

9898
#ifndef WINCOMPAT
99-
static FILE *userspace_interface_file(const char *interface)
99+
static FILE *userspace_interface_file(const char *iface)
100100
{
101101
struct stat sbuf;
102102
struct sockaddr_un addr = { .sun_family = AF_UNIX };
103103
int fd = -1, ret;
104104
FILE *f = NULL;
105105

106106
errno = EINVAL;
107-
if (strchr(interface, '/'))
107+
if (strchr(iface, '/'))
108108
goto out;
109-
ret = snprintf(addr.sun_path, sizeof(addr.sun_path), SOCK_PATH "%s" SOCK_SUFFIX, interface);
109+
ret = snprintf(addr.sun_path, sizeof(addr.sun_path), SOCK_PATH "%s" SOCK_SUFFIX, iface);
110110
if (ret < 0)
111111
goto out;
112112
ret = stat(addr.sun_path, &sbuf);
@@ -140,15 +140,15 @@ static FILE *userspace_interface_file(const char *interface)
140140
return f;
141141
}
142142

143-
static bool userspace_has_wireguard_interface(const char *interface)
143+
static bool userspace_has_wireguard_interface(const char *iface)
144144
{
145145
struct stat sbuf;
146146
struct sockaddr_un addr = { .sun_family = AF_UNIX };
147147
int fd, ret;
148148

149-
if (strchr(interface, '/'))
149+
if (strchr(iface, '/'))
150150
return false;
151-
if (snprintf(addr.sun_path, sizeof(addr.sun_path), SOCK_PATH "%s" SOCK_SUFFIX, interface) < 0)
151+
if (snprintf(addr.sun_path, sizeof(addr.sun_path), SOCK_PATH "%s" SOCK_SUFFIX, iface) < 0)
152152
return false;
153153
if (stat(addr.sun_path, &sbuf) < 0)
154154
return false;
@@ -288,7 +288,7 @@ static int userspace_set_device(struct wgdevice *dev)
288288
num; \
289289
})
290290

291-
static int userspace_get_device(struct wgdevice **out, const char *interface)
291+
static int userspace_get_device(struct wgdevice **out, const char *iface)
292292
{
293293
struct wgdevice *dev;
294294
struct wgpeer *peer = NULL;
@@ -302,14 +302,14 @@ static int userspace_get_device(struct wgdevice **out, const char *interface)
302302
if (!dev)
303303
return -errno;
304304

305-
f = userspace_interface_file(interface);
305+
f = userspace_interface_file(iface);
306306
if (!f)
307307
return -errno;
308308

309309
fprintf(f, "get=1\n\n");
310310
fflush(f);
311311

312-
strncpy(dev->name, interface, IFNAMSIZ - 1);
312+
strncpy(dev->name, iface, IFNAMSIZ - 1);
313313
dev->name[IFNAMSIZ - 1] = '\0';
314314

315315
while (getline(&key, &line_buffer_len, f) > 0) {
@@ -889,7 +889,7 @@ static void coalesce_peers(struct wgdevice *device)
889889
}
890890
}
891891

892-
static int kernel_get_device(struct wgdevice **device, const char *interface)
892+
static int kernel_get_device(struct wgdevice **device, const char *iface)
893893
{
894894
int ret = 0;
895895
struct nlmsghdr *nlh;
@@ -908,7 +908,7 @@ static int kernel_get_device(struct wgdevice **device, const char *interface)
908908
}
909909

910910
nlh = mnlg_msg_prepare(nlg, WG_CMD_GET_DEVICE, NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP);
911-
mnl_attr_put_strz(nlh, WGDEVICE_A_IFNAME, interface);
911+
mnl_attr_put_strz(nlh, WGDEVICE_A_IFNAME, iface);
912912
if (mnlg_socket_send(nlg, nlh) < 0) {
913913
ret = -errno;
914914
goto out;
@@ -963,14 +963,14 @@ char *ipc_list_devices(void)
963963
return buffer.buffer;
964964
}
965965

966-
int ipc_get_device(struct wgdevice **dev, const char *interface)
966+
int ipc_get_device(struct wgdevice **dev, const char *iface)
967967
{
968968
#ifdef __linux__
969-
if (userspace_has_wireguard_interface(interface))
970-
return userspace_get_device(dev, interface);
971-
return kernel_get_device(dev, interface);
969+
if (userspace_has_wireguard_interface(iface))
970+
return userspace_get_device(dev, iface);
971+
return kernel_get_device(dev, iface);
972972
#else
973-
return userspace_get_device(dev, interface);
973+
return userspace_get_device(dev, iface);
974974
#endif
975975
}
976976

src/tools/wincompat/ipc.c

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,32 @@
55

66
#include <windows.h>
77
#include <tlhelp32.h>
8+
#include <accctrl.h>
9+
#include <aclapi.h>
810
#include <stdio.h>
911
#include <stdbool.h>
1012
#include <fcntl.h>
1113

12-
static FILE *userspace_interface_file(const char *interface)
14+
static FILE *userspace_interface_file(const char *iface)
1315
{
1416
char fname[MAX_PATH], error_message[1024 * 128] = { 0 };
1517
HANDLE thread_token, process_snapshot, winlogon_process, winlogon_token, duplicated_token, pipe_handle = INVALID_HANDLE_VALUE;
1618
PROCESSENTRY32 entry = { .dwSize = sizeof(PROCESSENTRY32) };
19+
PSECURITY_DESCRIPTOR pipe_sd;
20+
PSID pipe_sid;
21+
SID expected_sid;
1722
BOOL ret;
1823
int fd;
19-
DWORD last_error = ERROR_SUCCESS;
24+
DWORD last_error = ERROR_SUCCESS, bytes = sizeof(expected_sid);
2025
TOKEN_PRIVILEGES privileges = {
2126
.PrivilegeCount = 1,
2227
.Privileges = {{ .Attributes = SE_PRIVILEGE_ENABLED }}
2328
};
2429

2530
if (!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &privileges.Privileges[0].Luid))
2631
goto err;
32+
if (!CreateWellKnownSid(WinLocalSystemSid, NULL, &expected_sid, &bytes))
33+
goto err;
2734

2835
process_snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
2936
if (process_snapshot == INVALID_HANDLE_VALUE)
@@ -63,13 +70,24 @@ static FILE *userspace_interface_file(const char *interface)
6370
}
6471
CloseHandle(duplicated_token);
6572

66-
snprintf(fname, sizeof(fname), "\\\\.\\pipe\\WireGuard\\%s", interface);
73+
snprintf(fname, sizeof(fname), "\\\\.\\pipe\\WireGuard\\%s", iface);
6774
pipe_handle = CreateFile(fname, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
6875
last_error = GetLastError();
69-
if (pipe_handle != INVALID_HANDLE_VALUE) {
70-
last_error = ERROR_SUCCESS;
71-
break;
76+
if (pipe_handle == INVALID_HANDLE_VALUE)
77+
continue;
78+
last_error = GetSecurityInfo(pipe_handle, SE_FILE_OBJECT, OWNER_SECURITY_INFORMATION, &pipe_sid, NULL, NULL, NULL, &pipe_sd);
79+
if (last_error != ERROR_SUCCESS) {
80+
CloseHandle(pipe_handle);
81+
continue;
82+
}
83+
last_error = EqualSid(&expected_sid, pipe_sid) ? ERROR_SUCCESS : ERROR_ACCESS_DENIED;
84+
LocalFree(pipe_sd);
85+
if (last_error != ERROR_SUCCESS) {
86+
CloseHandle(pipe_handle);
87+
continue;
7288
}
89+
last_error = ERROR_SUCCESS;
90+
break;
7391
}
7492
RevertToSelf();
7593
CloseHandle(process_snapshot);

0 commit comments

Comments
 (0)