libceph: Fix potential out-of-bounds access in ceph_handle_auth_reply()
PATCH 1/1 libceph: Fix potential out-of-bounds access in ceph_handle_auth_reply()
This patch fixes an out-of-bounds access in ceph_handle_auth_reply() that can be triggered by a message of type CEPH_MSG_AUTH_REPLY. In ceph_handle_auth_reply(), the value of the payload_len field of such a message is stored in a variable of type int. A value greater than INT_MAX leads to an integer overflow and is interpreted as a negative value. This leads to decrementing the pointer address by this value and subsequently accessing it because ceph_decode_need() only checks that the memory access does not exceed the end address of the allocation.
This patch fixes the issue by changing the data type of payload_len to u32. Additionally, the data type of result_msg_len is changed to u32, as it is also a variable holding a non-negative length.
BUG: KASAN: slab-out-of-bounds in ceph_handle_auth_reply+0x642/0x7a0 [libceph] Read of size 4 at addr ffff88811404df14 by task kworker/20:1/262 CPU: 20 UID: 0 PID: 262 Comm: kworker/20:1 Not tainted 6.19.2 #5 PREEMPT(voluntary) Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014 Workqueue: ceph-msgr ceph_con_workfn [libceph] Call Trace: <TASK> dump_stack_lvl+0x76/0xa0 print_report+0xd1/0x620 ? __pfx__raw_spin_lock_irqsave+0x10/0x10 ? kasan_complete_mode_report_info+0x72/0x210 kasan_report+0xe7/0x130 ? ceph_handle_auth_reply+0x642/0x7a0 [libceph] ? ceph_handle_auth_reply+0x642/0x7a0 [libceph] __asan_report_load_n_noabort+0xf/0x20 ceph_handle_auth_reply+0x642/0x7a0 [libceph] mon_dispatch+0x973/0x23d0 [libceph] ? apparmor_socket_recvmsg+0x6b/0xa0 ? __pfx_mon_dispatch+0x10/0x10 [libceph] ? __kasan_check_write+0x14/0x30 ? mutex_unlock+0x7f/0xd0 ? __pfx_mutex_unlock+0x10/0x10 ? __pfx_do_recvmsg+0x10/0x10 [libceph] ceph_con_process_message+0x1f1/0x650 [libceph] process_message+0x1e/0x450 [libceph] ceph_con_v2_try_read+0x2e48/0x6c80 [libceph] ? __pfx_ceph_con_v2_try_read+0x10/0x10 [libceph] ? save_fpregs_to_fpstate+0xb0/0x230 ? raw_spin_rq_unlock+0x17/0xa0 ? finish_task_switch.isra.0+0x13b/0x760 ? __switch_to+0x385/0xda0 ? __kasan_check_write+0x14/0x30 ? mutex_lock+0x8d/0xe0 ? __pfx_mutex_lock+0x10/0x10 ceph_con_workfn+0x248/0x10c0 [libceph] process_one_work+0x629/0xf80 ? __kasan_check_write+0x14/0x30 worker_thread+0x87f/0x1570 ? __pfx__raw_spin_lock_irqsave+0x10/0x10 ? __pfx_try_to_wake_up+0x10/0x10 ? kasan_print_address_stack_frame+0x1f7/0x280 ? __pfx_worker_thread+0x10/0x10 kthread+0x396/0x830 ? __pfx__raw_spin_lock_irq+0x10/0x10 ? __pfx_kthread+0x10/0x10 ? __kasan_check_write+0x14/0x30 ? recalc_sigpending+0x180/0x210 ? __pfx_kthread+0x10/0x10 ret_from_fork+0x3f7/0x610 ? __pfx_ret_from_fork+0x10/0x10 ? __switch_to+0x385/0xda0 ? __pfx_kthread+0x10/0x10 ret_from_fork_asm+0x1a/0x30 </TASK>
--- net/ceph/auth.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/ceph/auth.c +2 -2
@@ -205,9 +205,9 @@ int ceph_handle_auth_reply(struct ceph_auth_client *ac,s32 result;u64 global_id;void *payload, *payload_end; -int payload_len; +u32 payload_len;char *result_msg; -int result_msg_len; +u32 result_msg_len;int ret = -EINVAL ;mutex_lock (&ac->mutex);
Signature · 1 line
2.47.3
Quoted 9 lines · Raphael Zimmer
This patch fixes an out-of-bounds access in
ceph_handle_auth_reply()that can be triggered by a message of typeCEPH_MSG_AUTH_REPLY. Inceph_handle_auth_reply(), the value of thepayload_lenfield of such a message is stored in a variable of type int. A value greater thanINT_MAXleads to an integer overflow and is interpreted as a negative value. This leads to decrementing the pointer address by this value and subsequently accessing it becauseceph_decode_need()only checks that the memory access does not exceed the end address of the allocation.
Do you mean that payload_len could be bigger than INT_MAX? Is it possible scenario to have so big payload size in one message?
Maybe, the problem is more serious than simple changing of int on u32? It sounds like we are hiding a real issue by this fix.
Thanks,
Slava.
Quoted 81 lines
This patch fixes the issue by changing the data type of
payload_lento u32. Additionally, the data type ofresult_msg_lenis changed to u32, as it is also a variable holding a non-negative length.BUG: KASAN: slab-out-of-bounds in ceph_handle_auth_reply+0x642/0x7a0 [libceph] Read of size 4 at addr ffff88811404df14 by task kworker/20:1/262 CPU: 20 UID: 0 PID: 262 Comm: kworker/20:1 Not tainted 6.19.2 #5 PREEMPT(voluntary) Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014 Workqueue: ceph-msgr ceph_con_workfn [libceph] Call Trace: <TASK> dump_stack_lvl+0x76/0xa0 print_report+0xd1/0x620 ? __pfx__raw_spin_lock_irqsave+0x10/0x10 ? kasan_complete_mode_report_info+0x72/0x210 kasan_report+0xe7/0x130 ? ceph_handle_auth_reply+0x642/0x7a0 [libceph] ? ceph_handle_auth_reply+0x642/0x7a0 [libceph] __asan_report_load_n_noabort+0xf/0x20 ceph_handle_auth_reply+0x642/0x7a0 [libceph] mon_dispatch+0x973/0x23d0 [libceph] ? apparmor_socket_recvmsg+0x6b/0xa0 ? __pfx_mon_dispatch+0x10/0x10 [libceph] ? __kasan_check_write+0x14/0x30 ? mutex_unlock+0x7f/0xd0 ? __pfx_mutex_unlock+0x10/0x10 ? __pfx_do_recvmsg+0x10/0x10 [libceph] ceph_con_process_message+0x1f1/0x650 [libceph] process_message+0x1e/0x450 [libceph] ceph_con_v2_try_read+0x2e48/0x6c80 [libceph] ? __pfx_ceph_con_v2_try_read+0x10/0x10 [libceph] ? save_fpregs_to_fpstate+0xb0/0x230 ? raw_spin_rq_unlock+0x17/0xa0 ? finish_task_switch.isra.0+0x13b/0x760 ? __switch_to+0x385/0xda0 ? __kasan_check_write+0x14/0x30 ? mutex_lock+0x8d/0xe0 ? __pfx_mutex_lock+0x10/0x10 ceph_con_workfn+0x248/0x10c0 [libceph] process_one_work+0x629/0xf80 ? __kasan_check_write+0x14/0x30 worker_thread+0x87f/0x1570 ? __pfx__raw_spin_lock_irqsave+0x10/0x10 ? __pfx_try_to_wake_up+0x10/0x10 ? kasan_print_address_stack_frame+0x1f7/0x280 ? __pfx_worker_thread+0x10/0x10 kthread+0x396/0x830 ? __pfx__raw_spin_lock_irq+0x10/0x10 ? __pfx_kthread+0x10/0x10 ? __kasan_check_write+0x14/0x30 ? recalc_sigpending+0x180/0x210 ? __pfx_kthread+0x10/0x10 ret_from_fork+0x3f7/0x610 ? __pfx_ret_from_fork+0x10/0x10 ? __switch_to+0x385/0xda0 ? __pfx_kthread+0x10/0x10 ret_from_fork_asm+0x1a/0x30 </TASK>Signed-off-by Raphael Zimmer <[email protected]>--- net/ceph/auth.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)net/ceph/auth.c +2
-2@@ -205,9 +205,9 @@ int ceph_handle_auth_reply(struct ceph_auth_client *ac,s32 result;u64 global_id;void *payload, *payload_end; -int payload_len; +u32 payload_len;char *result_msg; -int result_msg_len; +u32 result_msg_len;int ret = -EINVAL ;mutex_lock (&ac->mutex);
Quoted 11 lines · Viacheslav Dubeyko
Quoted 9 lines · Raphael Zimmer
This patch fixes an out-of-bounds access in
ceph_handle_auth_reply()that can be triggered by a message of typeCEPH_MSG_AUTH_REPLY. Inceph_handle_auth_reply(), the value of thepayload_lenfield of such a message is stored in a variable of type int. A value greater thanINT_MAXleads to an integer overflow and is interpreted as a negative value. This leads to decrementing the pointer address by this value and subsequently accessing it becauseceph_decode_need()only checks that the memory access does not exceed the end address of the allocation.
Quoted 9 lines
Do you mean that
payload_lencould be bigger thanINT_MAX? Is it possible scenario to have so big payload size in one message?Maybe, the problem is more serious than simple changing of int on u32? It sounds like we are hiding a real issue by this fix.
Thanks,
Slava.
Hi Slava,
actually, I don't think that a correctly functioning monitor will ever send an auth_reply message with a payload larger than 2GB. This patch shall prevent a (maliciously) corrupted message with a huge value in the payload_len field from causing a memory access to an out-of-bounds address.
Best regards,
Raphael
Quoted 22 lines · Raphael Zimmer
Quoted 11 lines · Viacheslav Dubeyko
Quoted 9 lines · Raphael Zimmer
This patch fixes an out-of-bounds access in
ceph_handle_auth_reply()that can be triggered by a message of typeCEPH_MSG_AUTH_REPLY. Inceph_handle_auth_reply(), the value of thepayload_lenfield of such a message is stored in a variable of type int. A value greater thanINT_MAXleads to an integer overflow and is interpreted as a negative value. This leads to decrementing the pointer address by this value and subsequently accessing it becauseceph_decode_need()only checks that the memory access does not exceed the end address of the allocation.Quoted 9 lines
Do you mean that
payload_lencould be bigger thanINT_MAX? Is it possible scenario to have so big payload size in one message?Maybe, the problem is more serious than simple changing of int on u32? It sounds like we are hiding a real issue by this fix.
Thanks,
Slava.
Quoted 6 lines
Hi Slava,
actually, I don't think that a correctly functioning monitor will ever send anauth_replymessage with a payload larger than 2GB. This patch shall prevent a (maliciously) corrupted message with a huge value in thepayload_lenfield from causing a memory access to an out-of-bounds address.
You've shared the Call Trace with the patch. So, I expected that you had a real crash.
OK. If we have the corrupted message with a huge value, then we need:
(1) Convert int into u32 -> you've already did this.
(2) It is dangerous to continue parsing the payload with corrupted payload_len because we definitely will try to access the out-of-bounds address during teh parsing. We need to check the correctness of payload_len. As far as I can see, ceph_handle_auth_reply() receives buf pointer and len value. I assume that payload_len cannot be bigger than len. I suggest to compare the payload_len and len. Does it make sense to you?
Thanks,
Slava.
Quoted 42 lines · Viacheslav Dubeyko
Quoted 22 lines · Raphael Zimmer
Quoted 11 lines · Viacheslav Dubeyko
Quoted 9 lines · Raphael Zimmer
This patch fixes an out-of-bounds access in
ceph_handle_auth_reply()that can be triggered by a message of typeCEPH_MSG_AUTH_REPLY. Inceph_handle_auth_reply(), the value of thepayload_lenfield of such a message is stored in a variable of type int. A value greater thanINT_MAXleads to an integer overflow and is interpreted as a negative value. This leads to decrementing the pointer address by this value and subsequently accessing it becauseceph_decode_need()only checks that the memory access does not exceed the end address of the allocation.Quoted 9 lines
Do you mean that
payload_lencould be bigger thanINT_MAX? Is it possible scenario to have so big payload size in one message?Maybe, the problem is more serious than simple changing of int on u32? It sounds like we are hiding a real issue by this fix.
Thanks,
Slava.Quoted 6 lines
Hi Slava,
actually, I don't think that a correctly functioning monitor will ever send anauth_replymessage with a payload larger than 2GB. This patch shall prevent a (maliciously) corrupted message with a huge value in thepayload_lenfield from causing a memory access to an out-of-bounds address.You've shared the Call Trace with the patch. So, I expected that you had a real crash.
OK. If we have the corrupted message with a huge value, then we need:
(1) Convert int into u32 -> you've already did this.
(2) It is dangerous to continue parsing the payload with corruptedpayload_lenbecause we definitely will try to access the out-of-bounds address during teh parsing. We need to check the correctness ofpayload_len. As far as I can see,ceph_handle_auth_reply()receives buf pointer and len value. I assume thatpayload_lencannot be bigger than len. I suggest to compare thepayload_lenand len. Does it make sense to you?
Quoted 2 lines
Thanks,
Slava.
Hi Slava,
I don't completely agree with your second point. In fact, payload_len should always be smaller than len. But with the current version of the patch, we will not try to access the out-of-bounds address during parsing if this doesn't hold true. That's because this check [1] ensures that the address does not lie beyond the end of the allocation. Changing the data type of payload_len to u32 ensures that the address can only be incremented and therefore only be inside the allocation or greater, but no longer smaller. The only exception from this would be if p += payload_len would lead to an overflow of p. This would require p to be already greater than 0xffffffff00000000 in the beginning. As far as I know, this is not possible because you can't get an address in this range from an allocation. Consequently, adding a check that compares payload_len to len would not hurt, but it's not necessary to fix the problem.
Best regards,
Raphael
[1] https://elixir.bootlin.com/linux/v7.0-rc1/source/net/ceph/auth.c#L222
Quoted 43 lines · Raphael Zimmer
Quoted 42 lines · Viacheslav Dubeyko
Quoted 22 lines · Raphael Zimmer
Quoted 11 lines · Viacheslav Dubeyko
Quoted 9 lines · Raphael Zimmer
This patch fixes an out-of-bounds access in
ceph_handle_auth_reply()that can be triggered by a message of typeCEPH_MSG_AUTH_REPLY. Inceph_handle_auth_reply(), the value of thepayload_lenfield of such a message is stored in a variable of type int. A value greater thanINT_MAXleads to an integer overflow and is interpreted as a negative value. This leads to decrementing the pointer address by this value and subsequently accessing it becauseceph_decode_need()only checks that the memory access does not exceed the end address of the allocation.Quoted 9 lines
Do you mean that
payload_lencould be bigger thanINT_MAX? Is it possible scenario to have so big payload size in one message?Maybe, the problem is more serious than simple changing of int on u32? It sounds like we are hiding a real issue by this fix.
Thanks,
Slava.Quoted 6 lines
Hi Slava,
actually, I don't think that a correctly functioning monitor will ever send anauth_replymessage with a payload larger than 2GB. This patch shall prevent a (maliciously) corrupted message with a huge value in thepayload_lenfield from causing a memory access to an out-of-bounds address.You've shared the Call Trace with the patch. So, I expected that you had a real crash.
OK. If we have the corrupted message with a huge value, then we need:
(1) Convert int into u32 -> you've already did this.
(2) It is dangerous to continue parsing the payload with corruptedpayload_lenbecause we definitely will try to access the out-of-bounds address during teh parsing. We need to check the correctness ofpayload_len. As far as I can see,ceph_handle_auth_reply()receives buf pointer and len value. I assume thatpayload_lencannot be bigger than len. I suggest to compare thepayload_lenand len. Does it make sense to you?
Quoted 6 lines
Quoted 2 lines
Thanks,
Slava.Hi Slava,
I don't completely agree with your second point. In fact,payload_lenshould always be smaller than len.
It sounds like contradiction to your initial point. :) Your initial point was that we "shall prevent a (maliciously) corrupted message with a huge value in the payload_len field from causing a memory access to an out-of-bounds address". If we are talking about corrupted message, then there is no guarantee that payload_len is smaller than len. Otherwise, if we have guarantee that payload_len is smaller than len, then even integer data type will be completely enough.
Quoted 11 lines
But with the current version of the
patch, we will not try to access the out-of-bounds address during parsing if this doesn't hold true. That's because this check [1] ensures that the address does not lie beyond the end of the allocation. Changing the data type ofpayload_lento u32 ensures that the address can only be incremented and therefore only be inside the allocation or greater, but no longer smaller. The only exception from this would be if p +=payload_lenwould lead to an overflow of p. This would require p to be already greater than0xffffffff00000000in the beginning. As far as I know, this is not possible because you can't get an address in this range from an allocation.
There is also no complete guarantee that pointer addresses will be sane. I prefer not to rely on sanity of address arithmetic. :)
Thanks,
Slava.
Quoted 7 lines
Consequently, adding a check that compares
payload_lento len would not hurt, but it's not necessary to fix the problem.Best regards,
Raphael
Quoted 51 lines · Viacheslav Dubeyko
Quoted 43 lines · Raphael Zimmer
Quoted 42 lines · Viacheslav Dubeyko
Quoted 22 lines · Raphael Zimmer
Quoted 11 lines · Viacheslav Dubeyko
Quoted 9 lines · Raphael Zimmer
This patch fixes an out-of-bounds access in
ceph_handle_auth_reply()that can be triggered by a message of typeCEPH_MSG_AUTH_REPLY. Inceph_handle_auth_reply(), the value of thepayload_lenfield of such a message is stored in a variable of type int. A value greater thanINT_MAXleads to an integer overflow and is interpreted as a negative value. This leads to decrementing the pointer address by this value and subsequently accessing it becauseceph_decode_need()only checks that the memory access does not exceed the end address of the allocation.Quoted 9 lines
Do you mean that
payload_lencould be bigger thanINT_MAX? Is it possible scenario to have so big payload size in one message?Maybe, the problem is more serious than simple changing of int on u32? It sounds like we are hiding a real issue by this fix.
Thanks,
Slava.Quoted 6 lines
Hi Slava,
actually, I don't think that a correctly functioning monitor will ever send anauth_replymessage with a payload larger than 2GB. This patch shall prevent a (maliciously) corrupted message with a huge value in thepayload_lenfield from causing a memory access to an out-of-bounds address.You've shared the Call Trace with the patch. So, I expected that you had a real crash.
OK. If we have the corrupted message with a huge value, then we need:
(1) Convert int into u32 -> you've already did this.
(2) It is dangerous to continue parsing the payload with corruptedpayload_lenbecause we definitely will try to access the out-of-bounds address during teh parsing. We need to check the correctness ofpayload_len. As far as I can see,ceph_handle_auth_reply()receives buf pointer and len value. I assume thatpayload_lencannot be bigger than len. I suggest to compare thepayload_lenand len. Does it make sense to you?Quoted 6 lines
Quoted 2 lines
Thanks,
Slava.Hi Slava,
I don't completely agree with your second point. In fact,payload_lenshould always be smaller than len.
Quoted 8 lines
It sounds like contradiction to your initial point. :) Your initial point was that we "shall prevent a (maliciously) corrupted message with a huge value in the
payload_lenfield from causing a memory access to an out-of-bounds address". If we are talking about corrupted message, then there is no guarantee thatpayload_lenis smaller than len. Otherwise, if we have guarantee thatpayload_lenis smaller than len, then even integer data type will be completely enough.
That's not what I meant with this sentence. I meant that you're right with "I assume that payload_len cannot be bigger than len." Checking that payload_len is smaller than len is a check that every valid message must pass. Subsequently, I argued why I think that this check doesn't give any meaningful additional protection regarding this issue.
Quoted 12 lines
Quoted 11 lines
But with the current version of the
patch, we will not try to access the out-of-bounds address during parsing if this doesn't hold true. That's because this check [1] ensures that the address does not lie beyond the end of the allocation. Changing the data type ofpayload_lento u32 ensures that the address can only be incremented and therefore only be inside the allocation or greater, but no longer smaller. The only exception from this would be if p +=payload_lenwould lead to an overflow of p. This would require p to be already greater than0xffffffff00000000in the beginning. As far as I know, this is not possible because you can't get an address in this range from an allocation.
Quoted 6 lines
There is also no complete guarantee that pointer addresses will be sane. I prefer not to rely on sanity of address arithmetic. :)
Thanks,
Slava.
In my opinion, when you have a message that comes over the network, you should be prepared for it to have any mathematically possible value in any field. Therefore, we shouldn't implicitly assume anything about either payload_len or len. But if the pointer you get from a memory allocation inside your system is invalid, you have lost anyways. Then the check for payload_len < len doesn't help anymore, because the pointer will be invalid even if this condition holds true.
Nevertheless, since you seem concerned about it, I will look into it once more to add an additional check.
Best regards,
Raphael
Quoted 7 lines
Quoted 7 lines
Consequently, adding a check that compares
payload_lento len would not hurt, but it's not necessary to fix the problem.Best regards,
Raphael