1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
|
/***********************************************************/
/* ntux: native translation und extension */
/* Copyright (C) 2016--2021 SysDeer Technologies, LLC */
/* Released under GPLv2 and GPLv3; see COPYING.NTUX. */
/***********************************************************/
#include <psxabi/sys_sysapi.h>
#include <psxabi/sys_stat.h>
#include <psxabi/sys_path.h>
#include <psxabi/sys_errno.h>
#include <psxxfi/xfi_base.h>
#include <psxxfi/xfi_acl.h>
#include <psxxfi/xfi_fs.h>
#include <psxxfi/xfi_ofd.h>
#include <psxxfi/xfi_path.h>
#include <psxxfi/xfi_unicode.h>
#include <ntapi/nt_object.h>
#include <ntapi/nt_acl.h>
#include <ntapi/nt_file.h>
#include <ntux/ntux.h>
#include "ntux_driver_impl.h"
#include "ntux_nolibc_impl.h"
#include "ntux_errinfo_impl.h"
#include "nolibc/stdbool.h"
#define __SID_SYSTEM {1,1,{{0,0,0,0,0,5}},{18}}
#define __SID_ADMINISTRATORS {1,2,{{0,0,0,0,0,5}},{32,544}}
#define NTUX_OPT_MODE_NEUTRAL (0)
#define NTUX_OPT_MODE_EQUAL (1)
#define NTUX_OPT_MODE_PLUS (2)
#define NTUX_OPT_MODE_MINUS (3)
#define NTUX_BUF_SIZE_64K (64*1024)
static const nt_sid sid_system = __SID_SYSTEM;
static const nt_sid_os sid_admins = __SID_ADMINISTRATORS;
struct ntux_sd_buffer {
nt_sd_common_buffer sd;
uint32_t exbuf[512];
};
struct ntux_ace_any {
nt_ace_header header;
uint32_t mask;
uint32_t sid_start;
};
static int ntux_cmd_chmod_ret(int fd, struct __ofd * ofd, void * hasync, int ret)
{
if (hasync)
__xfi_close_handle(hasync);
if (ofd)
__xfi_ofd_ref_dec(ofd);
if (fd >= 0)
__sys_close(fd);
return ret;
}
static nt_sid * ntux_cmd_chmod_sid_from_name(const char * name)
{
if (!strcmp(name,"Administrators"))
return (nt_sid *)&sid_admins;
else if (!strcmp(name,"SYSTEM"))
return (nt_sid *)&sid_system;
else
return 0;
}
int ntux_cmd_chmod(const struct ntux_driver_ctx * dctx, const char * dunit)
{
intptr_t ret;
int32_t status;
int fdout;
int fdcwd;
int finherit;
int optmode;
const char * strmode;
const char * chmode;
const unsigned char * unit;
char * ch;
struct ntux_sd_buffer sdbuf;
struct ntux_sd_buffer srcsd;
struct ntux_sd_buffer cntsd;
nt_sd_common_buffer * dstsd;
nt_sd_common_meta meta;
nt_sid * owner;
nt_sid * group;
nt_acl * dacl;
struct ntux_ace_any * srcace;
struct ntux_ace_any * dstace;
uint32_t access_owner;
uint32_t access_group;
uint32_t access_other;
uint32_t access_admin;
uint32_t ace_flags;
uint32_t sec_mask;
size_t size;
size_t addr;
size_t aceaddr;
size_t cntbufsize;
char * cntpath_utf8;
wchar16_t * cntpath_utf16;
int exacefilter;
size_t exacesize;
int exacecount;
int idx;
int fd = -1;
int cntfd = -1;
struct __ofd * ofd = 0;
struct __ofd * cntofd = 0;
void * cntbuf = 0;
void * hasync = 0;
char dummy = 0;
/* strmode */
strmode = dctx->cctx->strmode ? dctx->cctx->strmode : &dummy;
optmode = NTUX_OPT_MODE_NEUTRAL;
finherit = false;
ace_flags = 0;
for (chmode=strmode; *chmode; chmode++) {
switch (*chmode) {
case '=':
optmode = NTUX_OPT_MODE_EQUAL;
finherit = 0;
ace_flags = 0;
break;
case '+':
optmode = NTUX_OPT_MODE_PLUS;
break;
case '-':
optmode = NTUX_OPT_MODE_MINUS;
break;
case 'p':
switch (optmode) {
case NTUX_OPT_MODE_EQUAL:
case NTUX_OPT_MODE_PLUS:
ace_flags = NT_ACE_CONTAINER_INHERIT | NT_ACE_OBJECT_INHERIT;
break;
case NTUX_OPT_MODE_MINUS:
ace_flags = 0;
break;
default:
return ntux_cmd_chmod_ret(
0,0,0,
NTUX_CUSTOM_ERROR(
dctx,
NTUX_ERR_FLEE_ERROR));
}
break;
case 'P':
switch (optmode) {
case NTUX_OPT_MODE_EQUAL:
case NTUX_OPT_MODE_PLUS:
finherit = true;
break;
case NTUX_OPT_MODE_MINUS:
finherit = false;
break;
default:
return ntux_cmd_chmod_ret(
0,0,0,
NTUX_CUSTOM_ERROR(
dctx,
NTUX_ERR_FLEE_ERROR));
}
break;
default:
return ntux_cmd_chmod_ret(
0,0,0,
NTUX_CUSTOM_ERROR(
dctx,
NTUX_ERR_FLEE_ERROR));
}
}
/* initial --owner and --group support: Administrators, SYSTEM */
owner = 0;
group = 0;
if (dctx->cctx->owner)
if (!(owner = ntux_cmd_chmod_sid_from_name(dctx->cctx->owner)))
return ntux_cmd_chmod_ret(
0,0,0,
NTUX_CUSTOM_ERROR(
dctx,
NTUX_ERR_NOT_IMPLEMENTED));
if (dctx->cctx->group)
if (!(group = ntux_cmd_chmod_sid_from_name(dctx->cctx->group)))
return ntux_cmd_chmod_ret(
0,0,0,
NTUX_CUSTOM_ERROR(
dctx,
NTUX_ERR_NOT_IMPLEMENTED));
/* init */
ntux_driver_set_ectx(
dctx,0,dunit);
unit = (const unsigned char *)dunit;
/* fdctx */
fdout = ntux_driver_fdout(dctx);
fdcwd = ntux_driver_fdcwd(dctx);
/* fd */
if ((ret = __sys_openat(fdcwd,unit,0,0)) < 0)
if (ntux_errno_set(dctx,ret))
return ntux_cmd_chmod_ret(
0,0,0,
NTUX_SYSTEM_ERROR(dctx));
fd = ret;
/* ofd */
if (!(ofd = __xfi_ofd_ref_inc(fd)))
return ntux_cmd_chmod_ret(
fd,0,0,
NTUX_CUSTOM_ERROR(
dctx,
NTUX_ERR_FLOW_ERROR));
/* hasync */
sec_mask = NT_SEC_READ_CONTROL;
sec_mask |= NT_SEC_WRITE_DAC;
sec_mask |= owner ? NT_SEC_WRITE_OWNER : 0;
if ((status = __xfi_fs_open_async(
&hasync,
ofd->info.hfile,0,
sec_mask,
NT_FILE_SHARE_READ
| NT_FILE_SHARE_WRITE
| NT_FILE_SHARE_DELETE)))
if (ntux_errno_set(dctx,EACCES))
return ntux_cmd_chmod_ret(
fd,ofd,0,
NTUX_SYSTEM_ERROR(dctx));
/* srcsd */
if ((status = __xfi_query_security_object(
hasync,
NT_OWNER_SECURITY_INFORMATION
| NT_GROUP_SECURITY_INFORMATION
| NT_DACL_SECURITY_INFORMATION,
&srcsd.sd.sd,sizeof(srcsd),&size)))
if (ntux_errno_set(dctx,ENXIO))
return ntux_cmd_chmod_ret(
fd,ofd,hasync,
NTUX_SYSTEM_ERROR(dctx));
if ((status = __xfi_acl_init_common_descriptor_meta(
&meta,&srcsd.sd.sd,
NT_ACL_INIT_COMMON_DESCRIPTION_META_STRICT_MODE)))
if (ntux_errno_set(dctx,EBADF))
return ntux_cmd_chmod_ret(
fd,ofd,hasync,
NTUX_SYSTEM_ERROR(dctx));
/* source permissions */
access_owner = meta.owner_ace ? meta.owner_ace->mask : 0;
access_group = meta.group_ace ? meta.group_ace->mask : 0;
access_other = meta.other_ace ? meta.other_ace->mask : 0;
access_admin = meta.admin_ace ? meta.admin_ace->mask : 0;
/* initial --strmode support, retaining previous options as needed */
if (!dctx->cctx->strmode) {
ace_flags |= meta.owner_ace ? meta.owner_ace->header.ace_flags : 0;
ace_flags |= meta.group_ace ? meta.group_ace->header.ace_flags : 0;
ace_flags |= meta.other_ace ? meta.other_ace->header.ace_flags : 0;
ace_flags |= meta.admin_ace ? meta.admin_ace->header.ace_flags : 0;
}
/* updated dacl */
dstsd = &sdbuf.sd;
__xfi_acl_init_common_descriptor(
dstsd,
owner ? owner : meta.owner,
group ? group : meta.group,
0,0,
access_owner,access_group,access_other,
access_admin,meta.system_acc,
ace_flags);
if ((dstsd->sd.offset_dacl < dstsd->sd.offset_owner)
|| (dstsd->sd.offset_dacl < dstsd->sd.offset_group)
|| (dstsd->sd.offset_dacl < dstsd->sd.offset_sacl))
return ntux_cmd_chmod_ret(
fd,0,0,
NTUX_CUSTOM_ERROR(
dctx,
NTUX_ERR_FLOW_ERROR));
/* inherited ace's */
if (finherit) {
dstsd->sd.control = NT_SE_SELF_RELATIVE
| NT_SE_DACL_AUTO_INHERIT_REQ
| NT_SE_DACL_AUTO_INHERITED
| NT_SE_DACL_PRESENT;
/* cntofd */
cntbuf = 0; cntbufsize = NTUX_BUF_SIZE_64K;
if (__xfi_allocate_virtual_memory(
NT_CURRENT_PROCESS_HANDLE,
&cntbuf,
0,
&cntbufsize,
NT_MEM_COMMIT,
NT_PAGE_READWRITE))
return ntux_cmd_chmod_ret(
fd,ofd,hasync,
NTUX_SYSTEM_ERROR(dctx));
if (__xfi_ofd_is_dir(ofd)) {
cntpath_utf16 = (wchar16_t *)cntbuf;
__xfi_path_conv_ofdat_utf16(
ofd,
(const wchar16_t[]){'.','.','/',0},
PSX_PATH_SYNTAX_ANY,
4*sizeof(wchar16_t),
cntpath_utf16,
PSX_PATH_SYNTAX_POSIX_STRICT,
cntbufsize,
0,0,PSX_PATH_CONV_RESOLVE_ELEMENT,
&cntofd);
} else {
cntpath_utf8 = cntbuf;
status = __xfi_path_conv_utf8(
fdcwd,
unit,
PSX_PATH_SYNTAX_POSIX_STRICT,
ntux_strlen((const char *)unit),
cntpath_utf8,
PSX_PATH_SYNTAX_POSIX_STRICT,
cntbufsize,
0,0,PSX_PATH_CONV_RESOLVE_ELEMENT,
0);
if (status) {
__xfi_free_virtual_memory(
NT_CURRENT_PROCESS_HANDLE,
&cntbuf,
&cntbufsize,
NT_MEM_RELEASE);
return ntux_cmd_chmod_ret(
fd,ofd,hasync,
NTUX_CUSTOM_ERROR(
dctx,
NTUX_ERR_FLOW_ERROR));
}
if ((cntpath_utf8[0] == '/') && cntpath_utf8[1]) {
for (ch=cntpath_utf8; *ch; ch++)
(void)0;
/* guard against an internal error */
if (ch[-1] != '/') {
for (--ch; *ch != '/'; ch--)
(void)0;
*ch = 0;
cntfd = __sys_openat(
fdcwd,
(const unsigned char *)cntpath_utf8,
0,0);
if (cntfd >= 0)
if (!(cntofd = __xfi_ofd_ref_inc(cntfd)))
__sys_close(cntfd);
}
}
}
__xfi_free_virtual_memory(
NT_CURRENT_PROCESS_HANDLE,
&cntbuf,
&cntbufsize,
NT_MEM_RELEASE);
if (!cntofd)
return ntux_cmd_chmod_ret(
fd,ofd,hasync,
NTUX_CUSTOM_ERROR(
dctx,
NTUX_ERR_FLOW_ERROR));
status = __xfi_query_security_object(
cntofd->info.hfile,
NT_OWNER_SECURITY_INFORMATION
| NT_GROUP_SECURITY_INFORMATION
| NT_DACL_SECURITY_INFORMATION,
&cntsd.sd.sd,sizeof(cntsd),&size);
if (status) {
__xfi_ofd_ref_dec(cntofd);
__sys_close(cntfd);
return ntux_cmd_chmod_ret(
fd,ofd,hasync,
NTUX_SYSTEM_ERROR(dctx));
}
/* exacefilter */
exacefilter = __xfi_ofd_is_dir(ofd)
? NT_ACE_CONTAINER_INHERIT
: NT_ACE_OBJECT_INHERIT;
/* exacecount, exacesize */
if (cntsd.sd.sd.offset_dacl) {
addr = (size_t)&cntsd.sd.sd;
addr += cntsd.sd.sd.offset_dacl;
dacl = (nt_acl *)addr;
addr += sizeof(*dacl);
exacecount = 0;
exacesize = 0;
for (idx=0; idx<dacl->ace_count; idx++) {
srcace = (struct ntux_ace_any *)addr;
if (srcace->header.ace_flags & exacefilter) {
exacecount++;
exacesize += srcace->header.ace_size;
}
addr += srcace->header.ace_size;
}
/* dstsd::dacl */
addr = (size_t)dstsd;
addr += dstsd->sd.offset_dacl;
dacl = (nt_acl *)addr;
if (dstsd->sd.offset_dacl + dacl->acl_size + exacesize > sizeof(sdbuf)) {
__xfi_ofd_ref_dec(cntofd);
__sys_close(cntfd);
return ntux_cmd_chmod_ret(
fd,ofd,hasync,
NTUX_BUFFER_ERROR(dctx));
}
dacl->acl_size += exacesize;
dacl->ace_count += exacecount;
/* pointer to dstsd's next ace */
aceaddr = addr;
aceaddr += sizeof(*dacl);
for (idx=0; idx<dacl->ace_count; idx++) {
dstace = (struct ntux_ace_any *)aceaddr;
aceaddr += dstace->header.ace_size;
}
dstace = (struct ntux_ace_any *)aceaddr;
/* copy container's ace's according to filter */
addr = (size_t)&cntsd.sd.sd;
addr += cntsd.sd.sd.offset_dacl;
dacl = (nt_acl *)addr;
addr += sizeof(*dacl);
for (idx=0; idx<dacl->ace_count; idx++) {
srcace = (struct ntux_ace_any *)addr;
if (srcace->header.ace_flags & exacefilter) {
ntux_memcpy(dstace,srcace,srcace->header.ace_size);
dstace->header.ace_flags = NT_ACE_INHERITED | ace_flags;
aceaddr += srcace->header.ace_size;
dstace = (struct ntux_ace_any *)aceaddr;
}
addr += srcace->header.ace_size;
}
}
}
/* finalize */
sec_mask = NT_DACL_SECURITY_INFORMATION;
sec_mask |= owner ? NT_OWNER_SECURITY_INFORMATION : 0;
if ((status = __xfi_set_security_object(
hasync,sec_mask,&dstsd->sd)))
if (ntux_errno_set(dctx,EPERM))
return ntux_cmd_chmod_ret(
fd,ofd,hasync,
NTUX_SYSTEM_ERROR(dctx));
/* changes */
(void)fdout;
/* cntofd */
if (cntofd) {
__xfi_ofd_ref_dec(cntofd);
__sys_close(cntfd);
}
/* all done */
return ntux_cmd_chmod_ret(fd,ofd,hasync,0);
}
|