Skip to content

Commit 8ec9897

Browse files
dcarattikuba-moo
authored andcommitted
netlabel: fix RCU annotation for IPv4 options on socket creation
Xiumei reports the following splat when netlabel and TCP socket are used: ============================= WARNING: suspicious RCU usage 6.9.0-rc2+ #637 Not tainted ----------------------------- net/ipv4/cipso_ipv4.c:1880 suspicious rcu_dereference_protected() usage! other info that might help us debug this: rcu_scheduler_active = 2, debug_locks = 1 1 lock held by ncat/23333: #0: ffffffff906030c0 (rcu_read_lock){....}-{1:2}, at: netlbl_sock_setattr+0x25/0x1b0 stack backtrace: CPU: 11 PID: 23333 Comm: ncat Kdump: loaded Not tainted 6.9.0-rc2+ #637 Hardware name: Supermicro SYS-6027R-72RF/X9DRH-7TF/7F/iTF/iF, BIOS 3.0 07/26/2013 Call Trace: <TASK> dump_stack_lvl+0xa9/0xc0 lockdep_rcu_suspicious+0x117/0x190 cipso_v4_sock_setattr+0x1ab/0x1b0 netlbl_sock_setattr+0x13e/0x1b0 selinux_netlbl_socket_post_create+0x3f/0x80 selinux_socket_post_create+0x1a0/0x460 security_socket_post_create+0x42/0x60 __sock_create+0x342/0x3a0 __sys_socket_create.part.22+0x42/0x70 __sys_socket+0x37/0xb0 __x64_sys_socket+0x16/0x20 do_syscall_64+0x96/0x180 ? do_user_addr_fault+0x68d/0xa30 ? exc_page_fault+0x171/0x280 ? asm_exc_page_fault+0x22/0x30 entry_SYSCALL_64_after_hwframe+0x71/0x79 RIP: 0033:0x7fbc0ca3fc1b Code: 73 01 c3 48 8b 0d 05 f2 1b 00 f7 d8 64 89 01 48 83 c8 ff c3 66 2e 0f 1f 84 00 00 00 00 00 90 f3 0f 1e fa b8 29 00 00 00 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d d5 f1 1b 00 f7 d8 64 89 01 48 RSP: 002b:00007fff18635208 EFLAGS: 00000246 ORIG_RAX: 0000000000000029 RAX: ffffffffffffffda RBX: 0000000000000001 RCX: 00007fbc0ca3fc1b RDX: 0000000000000006 RSI: 0000000000000001 RDI: 0000000000000002 RBP: 000055d24f80f8a0 R08: 0000000000000003 R09: 0000000000000001 R10: 0000000000020000 R11: 0000000000000246 R12: 000055d24f80f8a0 R13: 0000000000000000 R14: 000055d24f80fb88 R15: 0000000000000000 </TASK> The current implementation of cipso_v4_sock_setattr() replaces IP options under the assumption that the caller holds the socket lock; however, such assumption is not true, nor needed, in selinux_socket_post_create() hook. Let all callers of cipso_v4_sock_setattr() specify the "socket lock held" condition, except selinux_socket_post_create() _ where such condition can safely be set as true even without holding the socket lock. Fixes: f6d8bd0 ("inet: add RCU protection to inet->opt") Reported-by: Xiumei Mu <[email protected]> Signed-off-by: Davide Caratti <[email protected]> Acked-by: Casey Schaufler <[email protected]> Acked-by: Paul Moore <[email protected]> Link: https://lore.kernel.org/r/f4260d000a3a55b9e8b6a3b4e3fffc7da9f82d41.1715359817.git.dcaratti@redhat.com Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 1af7f88 commit 8ec9897

File tree

6 files changed

+52
-12
lines changed

6 files changed

+52
-12
lines changed

include/net/cipso_ipv4.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ int cipso_v4_getattr(const unsigned char *cipso,
183183
struct netlbl_lsm_secattr *secattr);
184184
int cipso_v4_sock_setattr(struct sock *sk,
185185
const struct cipso_v4_doi *doi_def,
186-
const struct netlbl_lsm_secattr *secattr);
186+
const struct netlbl_lsm_secattr *secattr,
187+
bool sk_locked);
187188
void cipso_v4_sock_delattr(struct sock *sk);
188189
int cipso_v4_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr);
189190
int cipso_v4_req_setattr(struct request_sock *req,
@@ -214,7 +215,8 @@ static inline int cipso_v4_getattr(const unsigned char *cipso,
214215

215216
static inline int cipso_v4_sock_setattr(struct sock *sk,
216217
const struct cipso_v4_doi *doi_def,
217-
const struct netlbl_lsm_secattr *secattr)
218+
const struct netlbl_lsm_secattr *secattr,
219+
bool sk_locked)
218220
{
219221
return -ENOSYS;
220222
}

include/net/netlabel.h

+10-2
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,8 @@ void netlbl_bitmap_setbit(unsigned char *bitmap, u32 bit, u8 state);
470470
int netlbl_enabled(void);
471471
int netlbl_sock_setattr(struct sock *sk,
472472
u16 family,
473-
const struct netlbl_lsm_secattr *secattr);
473+
const struct netlbl_lsm_secattr *secattr,
474+
bool sk_locked);
474475
void netlbl_sock_delattr(struct sock *sk);
475476
int netlbl_sock_getattr(struct sock *sk,
476477
struct netlbl_lsm_secattr *secattr);
@@ -487,6 +488,7 @@ int netlbl_skbuff_getattr(const struct sk_buff *skb,
487488
u16 family,
488489
struct netlbl_lsm_secattr *secattr);
489490
void netlbl_skbuff_err(struct sk_buff *skb, u16 family, int error, int gateway);
491+
bool netlbl_sk_lock_check(struct sock *sk);
490492

491493
/*
492494
* LSM label mapping cache operations
@@ -614,7 +616,8 @@ static inline int netlbl_enabled(void)
614616
}
615617
static inline int netlbl_sock_setattr(struct sock *sk,
616618
u16 family,
617-
const struct netlbl_lsm_secattr *secattr)
619+
const struct netlbl_lsm_secattr *secattr,
620+
bool sk_locked)
618621
{
619622
return -ENOSYS;
620623
}
@@ -673,6 +676,11 @@ static inline struct audit_buffer *netlbl_audit_start(int type,
673676
{
674677
return NULL;
675678
}
679+
680+
static inline bool netlbl_sk_lock_check(struct sock *sk)
681+
{
682+
return true;
683+
}
676684
#endif /* CONFIG_NETLABEL */
677685

678686
const struct netlbl_calipso_ops *

net/ipv4/cipso_ipv4.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -1815,6 +1815,7 @@ static int cipso_v4_genopt(unsigned char *buf, u32 buf_len,
18151815
* @sk: the socket
18161816
* @doi_def: the CIPSO DOI to use
18171817
* @secattr: the specific security attributes of the socket
1818+
* @sk_locked: true if caller holds the socket lock
18181819
*
18191820
* Description:
18201821
* Set the CIPSO option on the given socket using the DOI definition and
@@ -1826,7 +1827,8 @@ static int cipso_v4_genopt(unsigned char *buf, u32 buf_len,
18261827
*/
18271828
int cipso_v4_sock_setattr(struct sock *sk,
18281829
const struct cipso_v4_doi *doi_def,
1829-
const struct netlbl_lsm_secattr *secattr)
1830+
const struct netlbl_lsm_secattr *secattr,
1831+
bool sk_locked)
18301832
{
18311833
int ret_val = -EPERM;
18321834
unsigned char *buf = NULL;
@@ -1876,8 +1878,7 @@ int cipso_v4_sock_setattr(struct sock *sk,
18761878

18771879
sk_inet = inet_sk(sk);
18781880

1879-
old = rcu_dereference_protected(sk_inet->inet_opt,
1880-
lockdep_sock_is_held(sk));
1881+
old = rcu_dereference_protected(sk_inet->inet_opt, sk_locked);
18811882
if (inet_test_bit(IS_ICSK, sk)) {
18821883
sk_conn = inet_csk(sk);
18831884
if (old)

net/netlabel/netlabel_kapi.c

+28-3
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,7 @@ int netlbl_enabled(void)
965965
* @sk: the socket to label
966966
* @family: protocol family
967967
* @secattr: the security attributes
968+
* @sk_locked: true if caller holds the socket lock
968969
*
969970
* Description:
970971
* Attach the correct label to the given socket using the security attributes
@@ -977,7 +978,8 @@ int netlbl_enabled(void)
977978
*/
978979
int netlbl_sock_setattr(struct sock *sk,
979980
u16 family,
980-
const struct netlbl_lsm_secattr *secattr)
981+
const struct netlbl_lsm_secattr *secattr,
982+
bool sk_locked)
981983
{
982984
int ret_val;
983985
struct netlbl_dom_map *dom_entry;
@@ -997,7 +999,7 @@ int netlbl_sock_setattr(struct sock *sk,
997999
case NETLBL_NLTYPE_CIPSOV4:
9981000
ret_val = cipso_v4_sock_setattr(sk,
9991001
dom_entry->def.cipso,
1000-
secattr);
1002+
secattr, sk_locked);
10011003
break;
10021004
case NETLBL_NLTYPE_UNLABELED:
10031005
ret_val = 0;
@@ -1090,6 +1092,28 @@ int netlbl_sock_getattr(struct sock *sk,
10901092
return ret_val;
10911093
}
10921094

1095+
/**
1096+
* netlbl_sk_lock_check - Check if the socket lock has been acquired.
1097+
* @sk: the socket to be checked
1098+
*
1099+
* Return: true if socket @sk is locked or if lock debugging is disabled at
1100+
* runtime or compile-time; false otherwise
1101+
*
1102+
*/
1103+
#ifdef CONFIG_LOCKDEP
1104+
bool netlbl_sk_lock_check(struct sock *sk)
1105+
{
1106+
if (debug_locks)
1107+
return lockdep_sock_is_held(sk);
1108+
return true;
1109+
}
1110+
#else
1111+
bool netlbl_sk_lock_check(struct sock *sk)
1112+
{
1113+
return true;
1114+
}
1115+
#endif
1116+
10931117
/**
10941118
* netlbl_conn_setattr - Label a connected socket using the correct protocol
10951119
* @sk: the socket to label
@@ -1126,7 +1150,8 @@ int netlbl_conn_setattr(struct sock *sk,
11261150
switch (entry->type) {
11271151
case NETLBL_NLTYPE_CIPSOV4:
11281152
ret_val = cipso_v4_sock_setattr(sk,
1129-
entry->cipso, secattr);
1153+
entry->cipso, secattr,
1154+
netlbl_sk_lock_check(sk));
11301155
break;
11311156
case NETLBL_NLTYPE_UNLABELED:
11321157
/* just delete the protocols we support for right now

security/selinux/netlabel.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,10 @@ int selinux_netlbl_socket_post_create(struct sock *sk, u16 family)
402402
secattr = selinux_netlbl_sock_genattr(sk);
403403
if (secattr == NULL)
404404
return -ENOMEM;
405-
rc = netlbl_sock_setattr(sk, family, secattr);
405+
/* On socket creation, replacement of IP options is safe even if
406+
* the caller does not hold the socket lock.
407+
*/
408+
rc = netlbl_sock_setattr(sk, family, secattr, true);
406409
switch (rc) {
407410
case 0:
408411
sksec->nlbl_state = NLBL_LABELED;

security/smack/smack_lsm.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -2565,7 +2565,8 @@ static int smack_netlbl_add(struct sock *sk)
25652565
local_bh_disable();
25662566
bh_lock_sock_nested(sk);
25672567

2568-
rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel);
2568+
rc = netlbl_sock_setattr(sk, sk->sk_family, &skp->smk_netlabel,
2569+
netlbl_sk_lock_check(sk));
25692570
switch (rc) {
25702571
case 0:
25712572
ssp->smk_state = SMK_NETLBL_LABELED;

0 commit comments

Comments
 (0)