libceph: rework behavior for not recognized tag codes
Subsystems (2)
Viacheslav Dubeyko
maintainerPATCH 1/1 libceph: rework behavior for not recognized tag codes
From: Viacheslav Dubeyko <[email protected]>
[ 196.609307] WARNING: net/ceph/messenger_v1.c:901 at ceph_con_v1_try_read+0x4e0f/0x6020, CPU#6: kworker/6:0/49 [ 196.612062] Modules linked in: [ 196.613111] CPU: 6 UID: 0 PID: 49 Comm: kworker/6:0 Not tainted 6.19.0-g990dca9c3b7a-dirty #47 PREEMPT(lazy) [ 196.615707] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1ubuntu1.1 04/01/2014 [ 196.618102] Workqueue: ceph-msgr ceph_con_workfn [ 196.619417] RIP: 0010:ceph_con_v1_try_read+0x4e0f/0x6020 [ 196.620919] Code: 85 a9 fc 90 0f 0b e8 c0 85 a9 fc 90 0f 0b 90 e9 c4 e0 ff ff e8 b2 85 a9 fc 90 0f 0b e8 aa 85 a9 fc 90 0f 0b e8 a2 85 a9 fc 90 <0f> 0b 90 e9 2b e2 ff ff e8 94 85 a9 fc 90 0f 0b 48 8b 3c 24 e8 68 [ 196.625686] RSP: 0018:ff11000100e4fa30 EFLAGS: 00010293 [ 196.627142] RAX: 0000000000000000 RBX: 000000000000001d RCX: ffffffff9eb8ba8e [ 196.629041] RDX: ff11000100e04480 RSI: 000000000000001d RDI: 0000000000000004 [ 196.630938] RBP: 0000000075184a6c R08: 0000000000000001 R09: 0000000000000001 [ 196.632874] R10: 0000000075184a6c R11: ff11000100e04480 R12: 000000000000000d [ 196.634763] R13: ff1100012698a533 R14: ff1100012698a0b8 R15: 0000000000000000 [ 196.636667] FS: 0000000000000000(0000) GS:ff1100040bc00000(0000) knlGS:0000000000000000 [ 196.638807] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 196.640390] CR2: 00007f3f19a5f3b0 CR3: 00000003408a8006 CR4: 0000000000771ef0 [ 196.642293] PKRU: 55555554 [ 196.643105] Call Trace: [ 196.643782] <TASK> [ 196.644481] ? register_lock_class+0x41/0x760 [ 196.645739] ? srso_alias_return_thunk+0x5/0xfbef5 [ 196.647303] ? srso_alias_return_thunk+0x5/0xfbef5 [ 196.648896] ? __pfx___mutex_lock+0x10/0x10 [ 196.649986] ? __lock_acquire+0x466/0x2260 [ 196.650770] ? __pfx_ceph_con_v1_try_read+0x10/0x10 [ 196.651784] ceph_con_workfn+0xa26/0x1340 [ 196.652617] process_one_work+0x8fc/0x1990 [ 196.653496] ? __pfx_process_one_work+0x10/0x10 [ 196.654430] ? srso_alias_return_thunk+0x5/0xfbef5 [ 196.655393] ? lock_is_held_type+0x8f/0x100 [ 196.656260] ? srso_alias_return_thunk+0x5/0xfbef5 [ 196.657229] worker_thread+0x6d3/0xf20 [ 196.658062] ? __pfx_worker_thread+0x10/0x10 [ 196.658904] kthread+0x37d/0x490 [ 196.659590] ? lockdep_hardirqs_on_prepare+0xea/0x1a0 [ 196.660573] ? __pfx_kthread+0x10/0x10 [ 196.661370] ret_from_fork+0x67b/0xad0 [ 196.662167] ? __pfx_ret_from_fork+0x10/0x10 [ 196.663059] ? srso_alias_return_thunk+0x5/0xfbef5 [ 196.664008] ? __switch_to+0x7a0/0x10c0 [ 196.664751] ? __pfx_kthread+0x10/0x10 [ 196.665547] ret_from_fork_asm+0x1a/0x30 [ 196.666389] </TASK> [ 196.666822] irq event stamp: 9881 [ 196.667490] hardirqs last enabled at (9891): [<ffffffff9b40a7db>] __up_console_sem+0x7b/0x90 [ 196.669088] hardirqs last disabled at (9900): [<ffffffff9b40a7c0>] __up_console_sem+0x60/0x90 [ 196.670667] softirqs last enabled at (9664): [<ffffffff9b267a34>] __irq_exit_rcu+0xc4/0x100 [ 196.672258] softirqs last disabled at (9641): [<ffffffff9b267a34>] __irq_exit_rcu+0xc4/0x100 [ 196.673711] ---[ end trace 0000000000000000 ]--- [ 197.154025] libceph: mon0 (1)10.0.0.2:6789 socket closed (con state OPEN) [ 197.155329] libceph: mon0 (1)10.0.0.2:6789 session lost, hunting for new mon [ 197.158745] libceph: mon0 (1)10.0.0.2:6789 socket closed (con state V1_BANNER)
First issue:
After the client and server complete the v1 handshake and the server's reply tag is CEPH_MSGR_TAG_READY or CEPH_MSGR_TAG_SEQ, the client increments its own connect_seq and then checks that the server echoed back the same value. The con->v1.in_reply.connect_seq comes directly from the network — the server's connect reply message. A MITM who corrupts even one byte of that field produces a mismatch.
Using WARN_ON() on a value that comes from the wire means an attacker controls whether the kernel warning fires, giving them the ability to permanently taint the kernel (TAINT_WARN). WARN_ON() does not affect control flow — after the warning, the code continues and opens the connection as if everything were fine, with an inconsistent connect_seq.
The second issue:
The bad_tag is reached when the first byte of a new msgr-v1 frame is not one of the four recognised tag values (CEPH_MSGR_TAG_MSG, ACK, KEEPALIVE2_ACK, CLOSE). Any single-byte value 0–255 except those four reaches bad_tag. An attacker who can flood system log with error messages.
This patch compares con->v1.connect_seq and con->v1.in_reply.connect_seq. If they are not the same, then pr_err_ratelimited() will be called instead of WARN_ON(). The second fix exchanges pr_err() on pr_err_ratelimited() for the case of bad tag in ceph_con_v1_try_read().
--- net/ceph/messenger_v1.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
net/ceph/messenger_v1.c +10 -6
@@ -742,6 +742,7 @@ static int process_connect(struct ceph_connection *con)u64 sup_feat =from_msgr (con->msgr)->supported_features;u64 req_feat =from_msgr (con->msgr)->required_features;u64 server_feat =le64_to_cpu (con->v1.in_reply.features); +u32 connect_seq;int ret;dout ("process_connect on %p tag %d\n" , con, con->v1.in_tag);
@@ -894,12 +895,15 @@ static int process_connect(struct ceph_connection *con)le32_to_cpu (con->v1.in_reply.global_seq); con->v1.connect_seq++; con->peer_features = server_feat; + connect_seq =le32_to_cpu (con->v1.in_reply.connect_seq);dout ("process_connect got READY gseq %d cseq %d (%d)\n" , - con->v1.peer_global_seq, -le32_to_cpu (con->v1.in_reply.connect_seq), - con->v1.connect_seq); -WARN_ON (con->v1.connect_seq != -le32_to_cpu (con->v1.in_reply.connect_seq)); + con->v1.peer_global_seq, connect_seq, con->v1.connect_seq); +if (con->v1.connect_seq != connect_seq) { +pr_err_ratelimited ("connect_seq mismatch: my %u peer %u\n" , + con->v1.connect_seq, connect_seq); + con->error_msg ="protocol error, connect_seq mismatch" ; +return -1 ; + }if (con->v1.in_reply.flags &CEPH_MSG_CONNECT_LOSSY )ceph_con_flag_set (con,CEPH_CON_F_LOSSYTX );
@@ -1456,7 +1460,7 @@ int ceph_con_v1_try_read(struct ceph_connection *con)return ret; bad_tag: -pr_err ("try_read bad tag %d\n" , con->v1.in_tag); +pr_err_ratelimited ("try_read bad tag %d\n" , con->v1.in_tag); con->error_msg ="protocol error, garbage tag" ; ret =-1 ; goto out;
Signature · 1 line
2.54.0
Alex Markuze
maintainerHi Slava,
The hardening itself looks reasonable to me, but I have a couple of small comments before adding my Reviewed-by.
The Fixes tag looks like it points at the msgr1 field split rather than the commit that introduced the WARN_ON() behavior. If the original commit is hard to identify, I think it would be better to drop the Fixes tag and describe this as hardening.
Also, could you include the peer identity/address in the new connect_seq mismatch message? The nearby protocol feature mismatch errors include that context, and it would make this useful when multiple connections are active.
Splitting the connect_seq change and the bad_tag ratelimit into separate patches would also be cleaner, but I don't feel strongly about that.
Signature · 1 line
Alex Markuze