Commit 755662ce authored by Kuniyuki Iwashima's avatar Kuniyuki Iwashima Committed by Jakub Kicinski

af_unix: Use offsetof() instead of sizeof().

The length of the AF_UNIX socket address contains an offset to the member
sun_path of struct sockaddr_un.

Currently, the preceding member is just sun_family, and its type is
sa_family_t and resolved to short.  Therefore, the offset is represented by
sizeof(short).  However, it is not clear and fragile to changes in struct
sockaddr_storage or sockaddr_un.

This commit makes it clear and robust by rewriting sizeof() with
offsetof().
Signed-off-by: default avatarKuniyuki Iwashima <kuniyu@amazon.co.jp>
Signed-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent 442b03c3
...@@ -231,7 +231,8 @@ static int unix_mkname(struct sockaddr_un *sunaddr, int len, unsigned int *hashp ...@@ -231,7 +231,8 @@ static int unix_mkname(struct sockaddr_un *sunaddr, int len, unsigned int *hashp
{ {
*hashp = 0; *hashp = 0;
if (len <= sizeof(short) || len > sizeof(*sunaddr)) if (len <= offsetof(struct sockaddr_un, sun_path) ||
len > sizeof(*sunaddr))
return -EINVAL; return -EINVAL;
if (!sunaddr || sunaddr->sun_family != AF_UNIX) if (!sunaddr || sunaddr->sun_family != AF_UNIX)
return -EINVAL; return -EINVAL;
...@@ -244,7 +245,8 @@ static int unix_mkname(struct sockaddr_un *sunaddr, int len, unsigned int *hashp ...@@ -244,7 +245,8 @@ static int unix_mkname(struct sockaddr_un *sunaddr, int len, unsigned int *hashp
* kernel address buffer. * kernel address buffer.
*/ */
((char *)sunaddr)[len] = 0; ((char *)sunaddr)[len] = 0;
len = strlen(sunaddr->sun_path)+1+sizeof(short); len = strlen(sunaddr->sun_path) +
offsetof(struct sockaddr_un, sun_path) + 1;
return len; return len;
} }
...@@ -966,7 +968,8 @@ static int unix_autobind(struct socket *sock) ...@@ -966,7 +968,8 @@ static int unix_autobind(struct socket *sock)
goto out; goto out;
err = -ENOMEM; err = -ENOMEM;
addr = kzalloc(sizeof(*addr) + sizeof(short) + 16, GFP_KERNEL); addr = kzalloc(sizeof(*addr) +
offsetof(struct sockaddr_un, sun_path) + 16, GFP_KERNEL);
if (!addr) if (!addr)
goto out; goto out;
...@@ -974,7 +977,8 @@ static int unix_autobind(struct socket *sock) ...@@ -974,7 +977,8 @@ static int unix_autobind(struct socket *sock)
refcount_set(&addr->refcnt, 1); refcount_set(&addr->refcnt, 1);
retry: retry:
addr->len = sprintf(addr->name->sun_path+1, "%05x", ordernum) + 1 + sizeof(short); addr->len = sprintf(addr->name->sun_path + 1, "%05x", ordernum) +
offsetof(struct sockaddr_un, sun_path) + 1;
addr->hash = unix_hash_fold(csum_partial(addr->name, addr->len, 0)); addr->hash = unix_hash_fold(csum_partial(addr->name, addr->len, 0));
addr->hash ^= sk->sk_type; addr->hash ^= sk->sk_type;
...@@ -1156,7 +1160,7 @@ static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) ...@@ -1156,7 +1160,7 @@ static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
sunaddr->sun_family != AF_UNIX) sunaddr->sun_family != AF_UNIX)
return -EINVAL; return -EINVAL;
if (addr_len == sizeof(short)) if (addr_len == offsetof(struct sockaddr_un, sun_path))
return unix_autobind(sock); return unix_autobind(sock);
err = unix_mkname(sunaddr, addr_len, &hash); err = unix_mkname(sunaddr, addr_len, &hash);
...@@ -1600,7 +1604,7 @@ static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int peer) ...@@ -1600,7 +1604,7 @@ static int unix_getname(struct socket *sock, struct sockaddr *uaddr, int peer)
if (!addr) { if (!addr) {
sunaddr->sun_family = AF_UNIX; sunaddr->sun_family = AF_UNIX;
sunaddr->sun_path[0] = 0; sunaddr->sun_path[0] = 0;
err = sizeof(short); err = offsetof(struct sockaddr_un, sun_path);
} else { } else {
err = addr->len; err = addr->len;
memcpy(sunaddr, addr->name, addr->len); memcpy(sunaddr, addr->name, addr->len);
...@@ -3228,7 +3232,8 @@ static int unix_seq_show(struct seq_file *seq, void *v) ...@@ -3228,7 +3232,8 @@ static int unix_seq_show(struct seq_file *seq, void *v)
seq_putc(seq, ' '); seq_putc(seq, ' ');
i = 0; i = 0;
len = u->addr->len - sizeof(short); len = u->addr->len -
offsetof(struct sockaddr_un, sun_path);
if (!UNIX_ABSTRACT(s)) if (!UNIX_ABSTRACT(s))
len--; len--;
else { else {
......
...@@ -19,7 +19,8 @@ static int sk_diag_dump_name(struct sock *sk, struct sk_buff *nlskb) ...@@ -19,7 +19,8 @@ static int sk_diag_dump_name(struct sock *sk, struct sk_buff *nlskb)
if (!addr) if (!addr)
return 0; return 0;
return nla_put(nlskb, UNIX_DIAG_NAME, addr->len - sizeof(short), return nla_put(nlskb, UNIX_DIAG_NAME,
addr->len - offsetof(struct sockaddr_un, sun_path),
addr->name->sun_path); addr->name->sun_path);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment