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
|
From 1bdd7c438b399e2ecce9e3c72bd7c1ae56df60f8 Mon Sep 17 00:00:00 2001
From: Juergen Gross <jgross@suse.com>
Date: Tue, 13 Sep 2022 07:35:14 +0200
Subject: [PATCH 85/87] tools/xenstore: harden transaction finalization against
errors
When finalizing a transaction, any error occurring after checking for
conflicts will result in the transaction being performed only
partially today. Additionally accounting data will not be updated at
the end of the transaction, which might result in further problems
later.
Avoid those problems by multiple modifications:
- free any transaction specific nodes which don't need to be committed
as they haven't been written during the transaction as soon as their
generation count has been verified, this will reduce the risk of
out-of-memory situations
- store the transaction specific node name in struct accessed_node in
order to avoid the need to allocate additional memory for it when
finalizing the transaction
- don't stop the transaction finalization when hitting an error
condition, but try to continue to handle all modified nodes
- in case of a detected error do the accounting update as needed and
call the data base checking only after that
- if writing a node in a transaction is failing (e.g. due to a failed
quota check), fail the transaction, as prior changes to struct
accessed_node can't easily be undone in that case
This is part of XSA-421 / CVE-2022-42326.
Signed-off-by: Juergen Gross <jgross@suse.com>
Reviewed-by: Julien Grall <jgrall@amazon.com>
Tested-by: Julien Grall <jgrall@amazon.com>
(cherry picked from commit 2dd823ca7237e7fb90c890642d6a3b357a26fcff)
---
tools/xenstore/xenstored_core.c | 16 ++-
tools/xenstore/xenstored_transaction.c | 171 +++++++++++--------------
tools/xenstore/xenstored_transaction.h | 4 +-
3 files changed, 92 insertions(+), 99 deletions(-)
diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_core.c
index 041124d8b7a5..ccb7f0a92578 100644
--- a/tools/xenstore/xenstored_core.c
+++ b/tools/xenstore/xenstored_core.c
@@ -727,8 +727,7 @@ struct node *read_node(struct connection *conn, const void *ctx,
return NULL;
}
- if (transaction_prepend(conn, name, &key))
- return NULL;
+ transaction_prepend(conn, name, &key);
data = tdb_fetch(tdb_ctx, key);
@@ -846,10 +845,21 @@ int write_node_raw(struct connection *conn, TDB_DATA *key, struct node *node,
static int write_node(struct connection *conn, struct node *node,
bool no_quota_check)
{
+ int ret;
+
if (access_node(conn, node, NODE_ACCESS_WRITE, &node->key))
return errno;
- return write_node_raw(conn, &node->key, node, no_quota_check);
+ ret = write_node_raw(conn, &node->key, node, no_quota_check);
+ if (ret && conn && conn->transaction) {
+ /*
+ * Reverting access_node() is hard, so just fail the
+ * transaction.
+ */
+ fail_transaction(conn->transaction);
+ }
+
+ return ret;
}
unsigned int perm_for_conn(struct connection *conn,
diff --git a/tools/xenstore/xenstored_transaction.c b/tools/xenstore/xenstored_transaction.c
index 7ffe21bb5285..ac854197cadb 100644
--- a/tools/xenstore/xenstored_transaction.c
+++ b/tools/xenstore/xenstored_transaction.c
@@ -114,7 +114,8 @@ struct accessed_node
struct list_head list;
/* The name of the node. */
- char *node;
+ char *trans_name; /* Transaction specific name. */
+ char *node; /* Main data base name. */
/* Generation count (or NO_GENERATION) for conflict checking. */
uint64_t generation;
@@ -199,25 +200,20 @@ static char *transaction_get_node_name(void *ctx, struct transaction *trans,
* Prepend the transaction to name if node has been modified in the current
* transaction.
*/
-int transaction_prepend(struct connection *conn, const char *name,
- TDB_DATA *key)
+void transaction_prepend(struct connection *conn, const char *name,
+ TDB_DATA *key)
{
- char *tdb_name;
+ struct accessed_node *i;
- if (!conn || !conn->transaction ||
- !find_accessed_node(conn->transaction, name)) {
- set_tdb_key(name, key);
- return 0;
+ if (conn && conn->transaction) {
+ i = find_accessed_node(conn->transaction, name);
+ if (i) {
+ set_tdb_key(i->trans_name, key);
+ return;
+ }
}
- tdb_name = transaction_get_node_name(conn->transaction,
- conn->transaction, name);
- if (!tdb_name)
- return errno;
-
- set_tdb_key(tdb_name, key);
-
- return 0;
+ set_tdb_key(name, key);
}
/*
@@ -240,7 +236,6 @@ int access_node(struct connection *conn, struct node *node,
struct accessed_node *i = NULL;
struct transaction *trans;
TDB_DATA local_key;
- const char *trans_name = NULL;
int ret;
bool introduce = false;
@@ -259,10 +254,6 @@ int access_node(struct connection *conn, struct node *node,
trans = conn->transaction;
- trans_name = transaction_get_node_name(node, trans, node->name);
- if (!trans_name)
- goto nomem;
-
i = find_accessed_node(trans, node->name);
if (!i) {
if (trans->nodes >= quota_trans_nodes &&
@@ -273,9 +264,10 @@ int access_node(struct connection *conn, struct node *node,
i = talloc_zero(trans, struct accessed_node);
if (!i)
goto nomem;
- i->node = talloc_strdup(i, node->name);
- if (!i->node)
+ i->trans_name = transaction_get_node_name(i, trans, node->name);
+ if (!i->trans_name)
goto nomem;
+ i->node = strchr(i->trans_name, '/') + 1;
if (node->generation != NO_GENERATION && node->perms.num) {
i->perms.p = talloc_array(i, struct xs_permissions,
node->perms.num);
@@ -302,7 +294,7 @@ int access_node(struct connection *conn, struct node *node,
i->generation = node->generation;
i->check_gen = true;
if (node->generation != NO_GENERATION) {
- set_tdb_key(trans_name, &local_key);
+ set_tdb_key(i->trans_name, &local_key);
ret = write_node_raw(conn, &local_key, node, true);
if (ret)
goto err;
@@ -321,7 +313,7 @@ int access_node(struct connection *conn, struct node *node,
return -1;
if (key) {
- set_tdb_key(trans_name, key);
+ set_tdb_key(i->trans_name, key);
if (type == NODE_ACCESS_WRITE)
i->ta_node = true;
if (type == NODE_ACCESS_DELETE)
@@ -333,7 +325,6 @@ int access_node(struct connection *conn, struct node *node,
nomem:
ret = ENOMEM;
err:
- talloc_free((void *)trans_name);
talloc_free(i);
trans->fail = true;
errno = ret;
@@ -371,100 +362,90 @@ void queue_watches(struct connection *conn, const char *name, bool watch_exact)
* base.
*/
static int finalize_transaction(struct connection *conn,
- struct transaction *trans)
+ struct transaction *trans, bool *is_corrupt)
{
- struct accessed_node *i;
+ struct accessed_node *i, *n;
TDB_DATA key, ta_key, data;
struct xs_tdb_record_hdr *hdr;
uint64_t gen;
- char *trans_name;
- int ret;
- list_for_each_entry(i, &trans->accessed, list) {
- if (!i->check_gen)
- continue;
+ list_for_each_entry_safe(i, n, &trans->accessed, list) {
+ if (i->check_gen) {
+ set_tdb_key(i->node, &key);
+ data = tdb_fetch(tdb_ctx, key);
+ hdr = (void *)data.dptr;
+ if (!data.dptr) {
+ if (tdb_error(tdb_ctx) != TDB_ERR_NOEXIST)
+ return EIO;
+ gen = NO_GENERATION;
+ } else
+ gen = hdr->generation;
+ talloc_free(data.dptr);
+ if (i->generation != gen)
+ return EAGAIN;
+ }
- set_tdb_key(i->node, &key);
- data = tdb_fetch(tdb_ctx, key);
- hdr = (void *)data.dptr;
- if (!data.dptr) {
- if (tdb_error(tdb_ctx) != TDB_ERR_NOEXIST)
- return EIO;
- gen = NO_GENERATION;
- } else
- gen = hdr->generation;
- talloc_free(data.dptr);
- if (i->generation != gen)
- return EAGAIN;
+ /* Entries for unmodified nodes can be removed early. */
+ if (!i->modified) {
+ if (i->ta_node) {
+ set_tdb_key(i->trans_name, &ta_key);
+ if (do_tdb_delete(conn, &ta_key, NULL))
+ return EIO;
+ }
+ list_del(&i->list);
+ talloc_free(i);
+ }
}
while ((i = list_top(&trans->accessed, struct accessed_node, list))) {
- trans_name = transaction_get_node_name(i, trans, i->node);
- if (!trans_name)
- /* We are doomed: the transaction is only partial. */
- goto err;
-
- set_tdb_key(trans_name, &ta_key);
-
- if (i->modified) {
- set_tdb_key(i->node, &key);
- if (i->ta_node) {
- data = tdb_fetch(tdb_ctx, ta_key);
- if (!data.dptr)
- goto err;
+ set_tdb_key(i->node, &key);
+ if (i->ta_node) {
+ set_tdb_key(i->trans_name, &ta_key);
+ data = tdb_fetch(tdb_ctx, ta_key);
+ if (data.dptr) {
hdr = (void *)data.dptr;
hdr->generation = ++generation;
- ret = do_tdb_write(conn, &key, &data, NULL,
- true);
+ *is_corrupt |= do_tdb_write(conn, &key, &data,
+ NULL, true);
talloc_free(data.dptr);
+ if (do_tdb_delete(conn, &ta_key, NULL))
+ *is_corrupt = true;
} else {
- /*
- * A node having been created and later deleted
- * in this transaction will have no generation
- * information stored.
- */
- ret = (i->generation == NO_GENERATION)
- ? 0 : do_tdb_delete(conn, &key, NULL);
- }
- if (ret)
- goto err;
- if (i->fire_watch) {
- fire_watches(conn, trans, i->node, NULL,
- i->watch_exact,
- i->perms.p ? &i->perms : NULL);
+ *is_corrupt = true;
}
+ } else {
+ /*
+ * A node having been created and later deleted
+ * in this transaction will have no generation
+ * information stored.
+ */
+ *is_corrupt |= (i->generation == NO_GENERATION)
+ ? false
+ : do_tdb_delete(conn, &key, NULL);
}
+ if (i->fire_watch)
+ fire_watches(conn, trans, i->node, NULL, i->watch_exact,
+ i->perms.p ? &i->perms : NULL);
- if (i->ta_node && do_tdb_delete(conn, &ta_key, NULL))
- goto err;
list_del(&i->list);
talloc_free(i);
}
return 0;
-
-err:
- corrupt(conn, "Partial transaction");
- return EIO;
}
static int destroy_transaction(void *_transaction)
{
struct transaction *trans = _transaction;
struct accessed_node *i;
- char *trans_name;
TDB_DATA key;
wrl_ntransactions--;
trace_destroy(trans, "transaction");
while ((i = list_top(&trans->accessed, struct accessed_node, list))) {
if (i->ta_node) {
- trans_name = transaction_get_node_name(i, trans,
- i->node);
- if (trans_name) {
- set_tdb_key(trans_name, &key);
- do_tdb_delete(trans->conn, &key, NULL);
- }
+ set_tdb_key(i->trans_name, &key);
+ do_tdb_delete(trans->conn, &key, NULL);
}
list_del(&i->list);
talloc_free(i);
@@ -556,6 +537,7 @@ int do_transaction_end(const void *ctx, struct connection *conn,
{
const char *arg = onearg(in);
struct transaction *trans;
+ bool is_corrupt = false;
int ret;
if (!arg || (!streq(arg, "T") && !streq(arg, "F")))
@@ -579,13 +561,17 @@ int do_transaction_end(const void *ctx, struct connection *conn,
ret = transaction_fix_domains(trans, false);
if (ret)
return ret;
- if (finalize_transaction(conn, trans))
- return EAGAIN;
+ ret = finalize_transaction(conn, trans, &is_corrupt);
+ if (ret)
+ return ret;
wrl_apply_debit_trans_commit(conn);
/* fix domain entry for each changed domain */
transaction_fix_domains(trans, true);
+
+ if (is_corrupt)
+ corrupt(conn, "transaction inconsistency");
}
send_ack(conn, XS_TRANSACTION_END);
@@ -660,7 +646,7 @@ int check_transactions(struct hashtable *hash)
struct connection *conn;
struct transaction *trans;
struct accessed_node *i;
- char *tname, *tnode;
+ char *tname;
list_for_each_entry(conn, &connections, list) {
list_for_each_entry(trans, &conn->transaction_list, list) {
@@ -672,11 +658,8 @@ int check_transactions(struct hashtable *hash)
list_for_each_entry(i, &trans->accessed, list) {
if (!i->ta_node)
continue;
- tnode = transaction_get_node_name(tname, trans,
- i->node);
- if (!tnode || !remember_string(hash, tnode))
+ if (!remember_string(hash, i->trans_name))
goto nomem;
- talloc_free(tnode);
}
talloc_free(tname);
diff --git a/tools/xenstore/xenstored_transaction.h b/tools/xenstore/xenstored_transaction.h
index 39d7f81c5127..3417303f9427 100644
--- a/tools/xenstore/xenstored_transaction.h
+++ b/tools/xenstore/xenstored_transaction.h
@@ -48,8 +48,8 @@ int __must_check access_node(struct connection *conn, struct node *node,
void queue_watches(struct connection *conn, const char *name, bool watch_exact);
/* Prepend the transaction to name if appropriate. */
-int transaction_prepend(struct connection *conn, const char *name,
- TDB_DATA *key);
+void transaction_prepend(struct connection *conn, const char *name,
+ TDB_DATA *key);
/* Mark the transaction as failed. This will prevent it to be committed. */
void fail_transaction(struct transaction *trans);
--
2.37.4
|