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
|
--- source3/modules/vfs_streams_xattr.c.orig 2019-01-15 10:07:00 UTC
+++ source3/modules/vfs_streams_xattr.c
@@ -1,10 +1,10 @@
/*
* Store streams in xattrs
*
- * Copyright (C) Volker Lendecke, 2008
+ * Copyright (C) Volker Lendecke, 2008
+ * Copyright (C) Timur I. Bakeyev, 2017
*
* Partly based on James Peach's Darwin module, which is
- *
* Copyright (C) James Peach 2006-2007
*
* This program is free software; you can redistribute it and/or modify
@@ -79,25 +79,79 @@ static SMB_INO_T stream_inode(const SMB_
}
static ssize_t get_xattr_size(connection_struct *conn,
- const struct smb_filename *smb_fname,
- const char *xattr_name)
+ const struct smb_filename *smb_fname,
+ const char *xattr_name)
{
- NTSTATUS status;
- struct ea_struct ea;
ssize_t result;
- status = get_ea_value(talloc_tos(), conn, NULL, smb_fname,
- xattr_name, &ea);
+ result = SMB_VFS_GETXATTR(conn, smb_fname, xattr_name, NULL, 0);
+ // ? -1
+ return result;
+}
- if (!NT_STATUS_IS_OK(status)) {
- return -1;
+static NTSTATUS get_xattr_value(TALLOC_CTX *mem_ctx,
+ connection_struct *conn,
+ const struct smb_filename *smb_fname,
+ const char *ea_name,
+ struct ea_struct *pea)
+{
+ ssize_t attr_size;
+
+ attr_size = get_xattr_size(conn, smb_fname, ea_name);
+
+ if (attr_size == -1) {
+ return map_nt_error_from_unix(errno);
}
- result = ea.value.length-1;
- TALLOC_FREE(ea.value.data);
- return result;
+ pea->value = data_blob_talloc(mem_ctx, NULL, attr_size);
+ /* We may have xattr of a 0 size */
+ if(pea->value.data == NULL && attr_size) {
+ DEBUG(5,
+ ("get_xattr_value: for EA '%s' failed to allocate %lu bytes\n",
+ ea_name, (unsigned long)attr_size)
+ );
+ return NT_STATUS_NO_MEMORY;
+ }
+
+ attr_size = SMB_VFS_GETXATTR(conn, smb_fname, ea_name, pea->value.data, pea->value.length);
+
+ if (attr_size == -1) {
+ return map_nt_error_from_unix(errno);
+ }
+
+ if(pea->value.length != attr_size) {
+ DEBUG(5,
+ ("get_xattr_value: for EA '%s' requested %lu, read %lu bytes\n",
+ ea_name, (unsigned long)pea->value.length, (unsigned long)attr_size)
+ );
+ return NT_STATUS_UNSUCCESSFUL;
+ }
+
+ DEBUG(10,("get_xattr_value: EA '%s' is of length %lu\n", ea_name, (unsigned long)attr_size));
+ /*
+ * This can dump huge amount of data multiple times. For example
+ * for 1Mb ADS and chunk size 64Kb the same 1Mb dump will be
+ * logged 16 times!
+ */
+ dump_data(50, (uint8_t *)pea->value.data, pea->value.length);
+
+ pea->flags = 0;
+ // ? user.
+ if (strnequal(ea_name, "user.", 5)) {
+ pea->name = talloc_strdup(mem_ctx, &ea_name[5]);
+ } else {
+ pea->name = talloc_strdup(mem_ctx, ea_name);
+ }
+
+ if (pea->name == NULL) {
+ data_blob_free(&pea->value);
+ return NT_STATUS_NO_MEMORY;
+ }
+
+ return NT_STATUS_OK;
}
+
/**
* Given a stream name, populate xattr_name with the xattr name to use for
* accessing the stream.
@@ -114,6 +168,7 @@ static NTSTATUS streams_xattr_get_name(v
SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
return NT_STATUS_UNSUCCESSFUL);
+ // stream_name is passed as ':stream', so skip leading ':'
sname = talloc_strdup(ctx, stream_name + 1);
if (sname == NULL) {
return NT_STATUS_NO_MEMORY;
@@ -125,7 +180,7 @@ static NTSTATUS streams_xattr_get_name(v
* characters from their on-the-wire Unicode Private Range
* encoding to their native ASCII representation.
*
- * As as result the name of xattrs storing the streams (via
+ * As a result the name of xattrs storing the streams (via
* vfs_streams_xattr) may contain a colon, so we have to use
* strrchr_m() instead of strchr_m() for matching the stream
* type suffix.
@@ -157,7 +212,7 @@ static NTSTATUS streams_xattr_get_name(v
return NT_STATUS_NO_MEMORY;
}
- DEBUG(10, ("xattr_name: %s, stream_name: %s\n", *xattr_name,
+ DEBUG(10, ("xattr_name: '%s', stream_name: '%s'\n", *xattr_name,
stream_name));
talloc_free(sname);
@@ -270,8 +325,8 @@ static int streams_xattr_fstat(vfs_handl
return -1;
}
- sbuf->st_ex_size = get_xattr_size(handle->conn,
- smb_fname_base, io->xattr_name);
+ sbuf->st_ex_size = get_xattr_size(handle->conn, smb_fname_base,
+ io->xattr_name);
if (sbuf->st_ex_size == -1) {
TALLOC_FREE(smb_fname_base);
SET_STAT_INVALID(*sbuf);
@@ -446,10 +501,10 @@ static int streams_xattr_open(vfs_handle
goto fail;
}
- status = get_ea_value(talloc_tos(), handle->conn, NULL,
- smb_fname, xattr_name, &ea);
+ status = get_xattr_value(talloc_tos(), handle->conn,
+ smb_fname, xattr_name, &ea);
- DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
+ DEBUG(10, ("get_xattr_value returned %s\n", nt_errstr(status)));
if (!NT_STATUS_IS_OK(status)) {
if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
@@ -480,19 +535,13 @@ static int streams_xattr_open(vfs_handle
/*
* The attribute does not exist or needs to be truncated
*/
-
- /*
- * Darn, xattrs need at least 1 byte
- */
- char null = '\0';
-
DEBUG(10, ("creating or truncating attribute %s on file %s\n",
xattr_name, smb_fname->base_name));
ret = SMB_VFS_SETXATTR(fsp->conn,
smb_fname,
xattr_name,
- &null, sizeof(null),
+ NULL, 0,
flags & O_EXCL ? XATTR_CREATE : 0);
if (ret != 0) {
goto fail;
@@ -678,8 +727,8 @@ static int streams_xattr_rename(vfs_hand
}
/* read the old stream */
- status = get_ea_value(talloc_tos(), handle->conn, NULL,
- smb_fname_src, src_xattr_name, &ea);
+ status = get_xattr_value(talloc_tos(), handle->conn,
+ smb_fname_src, src_xattr_name, &ea);
if (!NT_STATUS_IS_OK(status)) {
errno = ENOENT;
goto fail;
@@ -766,14 +815,13 @@ static NTSTATUS walk_xattr_streams(vfs_h
continue;
}
- status = get_ea_value(names,
+ status = get_xattr_value(names,
handle->conn,
- NULL,
smb_fname,
names[i],
&ea);
if (!NT_STATUS_IS_OK(status)) {
- DEBUG(10, ("Could not get ea %s for file %s: %s\n",
+ DEBUG(10, ("Could not get EA %s for file %s: %s\n",
names[i],
smb_fname->base_name,
nt_errstr(status)));
@@ -835,16 +883,17 @@ struct streaminfo_state {
NTSTATUS status;
};
-static bool collect_one_stream(struct ea_struct *ea, void *private_data)
+static bool collect_one_stream(struct ea_struct *pea, void *private_data)
{
struct streaminfo_state *state =
(struct streaminfo_state *)private_data;
+ // ? -1
if (!add_one_stream(state->mem_ctx,
&state->num_streams, &state->streams,
- ea->name, ea->value.length-1,
+ pea->name, pea->value.length,
smb_roundup(state->handle->conn,
- ea->value.length-1))) {
+ pea->value.length))) {
state->status = NT_STATUS_NO_MEMORY;
return false;
}
@@ -964,14 +1013,17 @@ static ssize_t streams_xattr_pwrite(vfs_
files_struct *fsp, const void *data,
size_t n, off_t offset)
{
- struct stream_io *sio =
+ struct stream_io *sio =
(struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
+ struct smb_filename *smb_fname_base = NULL;
+ TALLOC_CTX *frame = NULL;
+
struct ea_struct ea;
NTSTATUS status;
- struct smb_filename *smb_fname_base = NULL;
int ret;
- DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
+ DEBUG(10, ("streams_xattr_pwrite: offset=%lu, size=%lu\n",
+ (unsigned long)offset, (unsigned long)n));
if (sio == NULL) {
return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
@@ -981,6 +1033,8 @@ static ssize_t streams_xattr_pwrite(vfs_
return -1;
}
+ frame = talloc_stackframe();
+
/* Create an smb_filename with stream_name == NULL. */
smb_fname_base = synthetic_smb_fname(talloc_tos(),
sio->base,
@@ -988,39 +1042,55 @@ static ssize_t streams_xattr_pwrite(vfs_
NULL,
fsp->fsp_name->flags);
if (smb_fname_base == NULL) {
+ TALLOC_FREE(frame);
errno = ENOMEM;
return -1;
}
- status = get_ea_value(talloc_tos(), handle->conn, NULL,
- smb_fname_base, sio->xattr_name, &ea);
- if (!NT_STATUS_IS_OK(status)) {
- return -1;
- }
-
- if ((offset + n) > ea.value.length-1) {
- uint8_t *tmp;
+ status = get_xattr_value(talloc_tos(), handle->conn,
+ smb_fname_base, sio->xattr_name, &ea);
- tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
- offset + n + 1);
+ if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
+ /*
+ * This can happen if we sit behind vfs_fruit:
+ * fruit_ftruncate calls UNLINK on an attribute
+ * truncating the "file" to zero length. A later
+ * pwrite faces a non-existing attribute, we need to
+ * cope with that here.
+ *
+ * This might be not the last word on this.
+ */
- if (tmp == NULL) {
- TALLOC_FREE(ea.value.data);
- errno = ENOMEM;
- return -1;
- }
- ea.value.data = tmp;
- ea.value.length = offset + n + 1;
- ea.value.data[offset+n] = 0;
- }
+ ea = (struct ea_struct) {0};
+ ea.name = talloc_strdup(talloc_tos(), sio->xattr_name);
+ if (ea.name == NULL) {
+ TALLOC_FREE(frame);
+ errno = ENOMEM;
+ return -1;
+ }
+ status = NT_STATUS_OK;
+ }
- memcpy(ea.value.data + offset, data, n);
+ if (!NT_STATUS_IS_OK(status)) {
+ TALLOC_FREE(frame);
+ return -1;
+ }
+ // ? -1
+ if ((offset + n) > ea.value.length) {
+ if(!data_blob_realloc(talloc_tos(), &ea.value, offset + n)) {
+ TALLOC_FREE(frame);
+ errno = ENOMEM;
+ return -1;
+ }
+ }
+ memcpy(ea.value.data + offset, data, n);
ret = SMB_VFS_SETXATTR(fsp->conn,
fsp->fsp_name,
sio->xattr_name,
ea.value.data, ea.value.length, 0);
- TALLOC_FREE(ea.value.data);
+
+ TALLOC_FREE(frame);
if (ret == -1) {
return -1;
@@ -1033,15 +1103,17 @@ static ssize_t streams_xattr_pread(vfs_h
files_struct *fsp, void *data,
size_t n, off_t offset)
{
- struct stream_io *sio =
+ struct stream_io *sio =
(struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
+ struct smb_filename *smb_fname_base = NULL;
+ TALLOC_CTX *frame = NULL;
+
struct ea_struct ea;
NTSTATUS status;
- size_t length, overlap;
- struct smb_filename *smb_fname_base = NULL;
+ size_t overlap;
- DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
- (int)offset, (int)n));
+ DEBUG(10, ("streams_xattr_pread: offset=%lu, size=%lu\n",
+ (unsigned long)offset, (unsigned long)n));
if (sio == NULL) {
return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
@@ -1051,6 +1123,8 @@ static ssize_t streams_xattr_pread(vfs_h
return -1;
}
+ frame = talloc_stackframe();
+
/* Create an smb_filename with stream_name == NULL. */
smb_fname_base = synthetic_smb_fname(talloc_tos(),
sio->base,
@@ -1058,31 +1132,35 @@ static ssize_t streams_xattr_pread(vfs_h
NULL,
fsp->fsp_name->flags);
if (smb_fname_base == NULL) {
+ TALLOC_FREE(frame);
errno = ENOMEM;
return -1;
}
- status = get_ea_value(talloc_tos(), handle->conn, NULL,
- smb_fname_base, sio->xattr_name, &ea);
+ status = get_xattr_value(talloc_tos(), handle->conn,
+ smb_fname_base, sio->xattr_name, &ea);
if (!NT_STATUS_IS_OK(status)) {
+ TALLOC_FREE(frame);
return -1;
}
+ // ? -1
+ //length = ea.value.length-1;
- length = ea.value.length-1;
+ DEBUG(10, ("streams_xattr_pread: get_xattr_value() returned %lu bytes\n",
+ (unsigned long)ea.value.length));
- DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
- (int)length));
+ /* Attempt to read past EOF. */
+ if (ea.value.length <= offset) {
+ TALLOC_FREE(frame);
+ return 0;
+ }
- /* Attempt to read past EOF. */
- if (length <= offset) {
- return 0;
- }
+ overlap = (offset + n) > ea.value.length ? (ea.value.length - offset) : n;
+ memcpy(data, ea.value.data + offset, overlap);
- overlap = (offset + n) > length ? (length - offset) : n;
- memcpy(data, ea.value.data + offset, overlap);
+ TALLOC_FREE(frame);
- TALLOC_FREE(ea.value.data);
- return overlap;
+ return overlap;
}
struct streams_xattr_pread_state {
@@ -1249,16 +1327,18 @@ static int streams_xattr_ftruncate(struc
struct files_struct *fsp,
off_t offset)
{
- int ret;
- uint8_t *tmp;
- struct ea_struct ea;
- NTSTATUS status;
- struct stream_io *sio =
+ struct stream_io *sio =
(struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
struct smb_filename *smb_fname_base = NULL;
+ TALLOC_CTX *frame = NULL;
- DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
- fsp_str_dbg(fsp), (double)offset));
+ struct ea_struct ea;
+ NTSTATUS status;
+ size_t orig_length;
+ int ret;
+
+ DEBUG(10, ("streams_xattr_ftruncate: called for file '%s' with offset %lu\n",
+ fsp_str_dbg(fsp), (unsigned long)offset));
if (sio == NULL) {
return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
@@ -1268,6 +1348,8 @@ static int streams_xattr_ftruncate(struc
return -1;
}
+ frame = talloc_stackframe();
+
/* Create an smb_filename with stream_name == NULL. */
smb_fname_base = synthetic_smb_fname(talloc_tos(),
sio->base,
@@ -1275,40 +1357,46 @@ static int streams_xattr_ftruncate(struc
NULL,
fsp->fsp_name->flags);
if (smb_fname_base == NULL) {
+ TALLOC_FREE(frame);
errno = ENOMEM;
return -1;
}
- status = get_ea_value(talloc_tos(), handle->conn, NULL,
- smb_fname_base, sio->xattr_name, &ea);
+ status = get_xattr_value(talloc_tos(), handle->conn,
+ smb_fname_base, sio->xattr_name, &ea);
if (!NT_STATUS_IS_OK(status)) {
+ TALLOC_FREE(frame);
return -1;
}
+ orig_length = ea.value.length;
- tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
- offset + 1);
+ /* Requested size matches the original size */
+ if(orig_length == offset) {
+ TALLOC_FREE(frame);
+ return 0;
+ }
- if (tmp == NULL) {
- TALLOC_FREE(ea.value.data);
+ /* That can both shrink and expand */
+ /* XXX: If offset == 0 the result of talloc_realloc is NULL, but still valid */
+ if(offset && !data_blob_realloc(talloc_tos(), &ea.value, offset)) {
+ TALLOC_FREE(frame);
errno = ENOMEM;
return -1;
}
- /* Did we expand ? */
- if (ea.value.length < offset + 1) {
- memset(&tmp[ea.value.length], '\0',
- offset + 1 - ea.value.length);
+ /* If we expanded, fill up extra space with zeros */
+ if (orig_length < offset) {
+ memset(ea.value.data + orig_length, 0,
+ offset - orig_length);
}
- ea.value.data = tmp;
- ea.value.length = offset + 1;
- ea.value.data[offset] = 0;
-
+ /* XXX: We should use ea.value.length here, but when offset == 0
+ it's not reset to 0 in data_blob_realloc() */
ret = SMB_VFS_SETXATTR(fsp->conn,
fsp->fsp_name,
sio->xattr_name,
- ea.value.data, ea.value.length, 0);
- TALLOC_FREE(ea.value.data);
+ ea.value.data, offset, 0);
+ TALLOC_FREE(frame);
if (ret == -1) {
return -1;
@@ -1326,9 +1414,9 @@ static int streams_xattr_fallocate(struc
struct stream_io *sio =
(struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
- DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
- "len = %.0f\n",
- fsp_str_dbg(fsp), (double)offset, (double)len));
+ DEBUG(10, ("streams_xattr_fallocate: called for file '%s' with offset %lu"
+ "len = %lu\n",
+ fsp_str_dbg(fsp), (unsigned long)offset, (unsigned long)len));
if (sio == NULL) {
return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
|