Skip to content

Commit

Permalink
net: add a CMSG_USER_DATA macro
Browse files Browse the repository at this point in the history
Add a variant of CMSG_DATA that operates on user pointer to avoid
sparse warnings about casting to/from user pointers.  Also fix up
CMSG_DATA to rely on the gcc extension that allows void pointer
arithmetics to cut down on the amount of casts.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Christoph Hellwig authored and David S. Miller committed May 11, 2020
1 parent 3242956 commit 0462b6b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
5 changes: 4 additions & 1 deletion include/linux/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ struct cmsghdr {

#define CMSG_ALIGN(len) ( ((len)+sizeof(long)-1) & ~(sizeof(long)-1) )

#define CMSG_DATA(cmsg) ((void *)((char *)(cmsg) + sizeof(struct cmsghdr)))
#define CMSG_DATA(cmsg) \
((void *)(cmsg) + sizeof(struct cmsghdr))
#define CMSG_USER_DATA(cmsg) \
((void __user *)(cmsg) + sizeof(struct cmsghdr))
#define CMSG_SPACE(len) (sizeof(struct cmsghdr) + CMSG_ALIGN(len))
#define CMSG_LEN(len) (sizeof(struct cmsghdr) + (len))

Expand Down
4 changes: 2 additions & 2 deletions net/core/scm.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data)
err = -EFAULT;
if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
goto out;
if (copy_to_user(CMSG_DATA(cm), data, cmlen - sizeof(struct cmsghdr)))
if (copy_to_user(CMSG_USER_DATA(cm), data, cmlen - sizeof(*cm)))
goto out;
cmlen = CMSG_SPACE(len);
if (msg->msg_controllen < cmlen)
Expand Down Expand Up @@ -300,7 +300,7 @@ void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm)
if (fdnum < fdmax)
fdmax = fdnum;

for (i=0, cmfptr=(__force int __user *)CMSG_DATA(cm); i<fdmax;
for (i=0, cmfptr =(int __user *)CMSG_USER_DATA(cm); i<fdmax;
i++, cmfptr++)
{
struct socket *sock;
Expand Down

0 comments on commit 0462b6b

Please sign in to comment.