Skip to content

Commit 5a287d3

Browse files
WOnder93pcmoore
authored andcommitted
lsm: fix default return value of the socket_getpeersec_*() hooks
For these hooks the true "neutral" value is -EOPNOTSUPP, which is currently what is returned when no LSM provides this hook and what LSMs return when there is no security context set on the socket. Correct the value in <linux/lsm_hooks.h> and adjust the dispatch functions in security/security.c to avoid issues when the BPF LSM is enabled. Cc: stable@vger.kernel.org Fixes: 98e828a ("security: Refactor declaration of LSM hooks") Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com> [PM: subject line tweak] Signed-off-by: Paul Moore <paul@paul-moore.com>
1 parent 99b817c commit 5a287d3

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

include/linux/lsm_hook_defs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,9 @@ LSM_HOOK(int, 0, socket_getsockopt, struct socket *sock, int level, int optname)
315315
LSM_HOOK(int, 0, socket_setsockopt, struct socket *sock, int level, int optname)
316316
LSM_HOOK(int, 0, socket_shutdown, struct socket *sock, int how)
317317
LSM_HOOK(int, 0, socket_sock_rcv_skb, struct sock *sk, struct sk_buff *skb)
318-
LSM_HOOK(int, 0, socket_getpeersec_stream, struct socket *sock,
318+
LSM_HOOK(int, -ENOPROTOOPT, socket_getpeersec_stream, struct socket *sock,
319319
sockptr_t optval, sockptr_t optlen, unsigned int len)
320-
LSM_HOOK(int, 0, socket_getpeersec_dgram, struct socket *sock,
320+
LSM_HOOK(int, -ENOPROTOOPT, socket_getpeersec_dgram, struct socket *sock,
321321
struct sk_buff *skb, u32 *secid)
322322
LSM_HOOK(int, 0, sk_alloc_security, struct sock *sk, int family, gfp_t priority)
323323
LSM_HOOK(void, LSM_RET_VOID, sk_free_security, struct sock *sk)

security/security.c

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4624,8 +4624,20 @@ EXPORT_SYMBOL(security_sock_rcv_skb);
46244624
int security_socket_getpeersec_stream(struct socket *sock, sockptr_t optval,
46254625
sockptr_t optlen, unsigned int len)
46264626
{
4627-
return call_int_hook(socket_getpeersec_stream, -ENOPROTOOPT, sock,
4628-
optval, optlen, len);
4627+
struct security_hook_list *hp;
4628+
int rc;
4629+
4630+
/*
4631+
* Only one module will provide a security context.
4632+
*/
4633+
hlist_for_each_entry(hp, &security_hook_heads.socket_getpeersec_stream,
4634+
list) {
4635+
rc = hp->hook.socket_getpeersec_stream(sock, optval, optlen,
4636+
len);
4637+
if (rc != LSM_RET_DEFAULT(socket_getpeersec_stream))
4638+
return rc;
4639+
}
4640+
return LSM_RET_DEFAULT(socket_getpeersec_stream);
46294641
}
46304642

46314643
/**
@@ -4645,8 +4657,19 @@ int security_socket_getpeersec_stream(struct socket *sock, sockptr_t optval,
46454657
int security_socket_getpeersec_dgram(struct socket *sock,
46464658
struct sk_buff *skb, u32 *secid)
46474659
{
4648-
return call_int_hook(socket_getpeersec_dgram, -ENOPROTOOPT, sock,
4649-
skb, secid);
4660+
struct security_hook_list *hp;
4661+
int rc;
4662+
4663+
/*
4664+
* Only one module will provide a security context.
4665+
*/
4666+
hlist_for_each_entry(hp, &security_hook_heads.socket_getpeersec_dgram,
4667+
list) {
4668+
rc = hp->hook.socket_getpeersec_dgram(sock, skb, secid);
4669+
if (rc != LSM_RET_DEFAULT(socket_getpeersec_dgram))
4670+
return rc;
4671+
}
4672+
return LSM_RET_DEFAULT(socket_getpeersec_dgram);
46504673
}
46514674
EXPORT_SYMBOL(security_socket_getpeersec_dgram);
46524675

0 commit comments

Comments
 (0)