selinux: use strlcpy() when copying IB device name
PATCH 1/1 selinux: use strlcpy() when copying IB device name
While the buffer should be large enough (IB_DEVICE_NAME_MAX) for all InfiniBand device names, it's better to be defensive and ensure the string will be null-terminated even if the hook happens to receive a longer name.
Found by a Coverity scan (BUFFER_SIZE warning).
--- security/selinux/hooks.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
security/selinux/hooks.c +1 -1
@@ -6864,7 +6864,7 @@ static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,return err; ad.type =LSM_AUDIT_DATA_IBENDPORT ; -strncpy (ibendport.dev_name, dev_name,sizeof (ibendport.dev_name)); +strlcpy (ibendport.dev_name, dev_name,sizeof (ibendport.dev_name)); ibendport.port = port_num; ad.u.ibendport = &ibendport; return avc_has_perm(&selinux_state,
Signature · 1 line
2.31.1
Paul Moore
maintainerQuoted 18 lines · Ondrej Mosnacek
While the buffer should be large enough (
IB_DEVICE_NAME_MAX) for all InfiniBand device names, it's better to be defensive and ensure the string will be null-terminated even if the hook happens to receive a longer name.Found by a Coverity scan (
BUFFER_SIZEwarning).Signed-off-by Ondrej Mosnacek <[email protected]>--- security/selinux/hooks.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)security/selinux/hooks.c +0
-0
security/selinux/hooks.c static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name, · line 6867
Quoted 5 lines
security/selinux/hooks.c +1
-1@@ -6864,7 +6864,7 @@ static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,return err; ad.type =LSM_AUDIT_DATA_IBENDPORT ; -strncpy (ibendport.dev_name, dev_name,sizeof (ibendport.dev_name)); +strlcpy (ibendport.dev_name, dev_name,sizeof (ibendport.dev_name));
The kernel preference these days appears to be to use strscpy() instead of strlcpy(); if we are going to change it, let's change it to strscpy().
Signature · 2 lines
paul moore www.paul-moore.com
Quoted 21 lines · Paul Moore
Quoted 20 lines · Ondrej Mosnacek
While the buffer should be large enough (
IB_DEVICE_NAME_MAX) for all InfiniBand device names, it's better to be defensive and ensure the string will be null-terminated even if the hook happens to receive a longer name.Found by a Coverity scan (
BUFFER_SIZEwarning).Signed-off-by Ondrej Mosnacek <[email protected]>--- security/selinux/hooks.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)security/selinux/hooks.c +0
-0@@ -6864,7 +6864,7 @@ static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,return err;
Quoted 7 lines
security/selinux/hooks.c static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name, · line 6867
Quoted 3 lines
security/selinux/hooks.c +1
-1ad.type =LSM_AUDIT_DATA_IBENDPORT ; -strncpy (ibendport.dev_name, dev_name,sizeof (ibendport.dev_name)); +strlcpy (ibendport.dev_name, dev_name,sizeof (ibendport.dev_name));The kernel preference these days appears to be to use
strscpy()instead ofstrlcpy(); if we are going to change it, let's change it tostrscpy().
Good point. But now that you made me look at it again, I noticed that we can simply turn the dev_name field to a const char * and avoid the copy altogether. The ibendport variable goes out of scope at the end of the function anyway, so the lifetime of the dev_name pointer will never be shorter than that of ibendport, thus we can safely just pass the dev_name pointer and be done with it.
So I'll update the patch to just switch to this other approach.
Signature · 3 lines
Ondrej Mosnacek Software Engineer, Linux Security - SELinux kernel Red Hat, Inc.