diff mbox

net: socket: don't set sk_uid to garbage value in ->setattr()

Message ID 20161230234232.4221-1-ebiggers3@gmail.com
State Accepted, archived
Delegated to: David Miller
Headers show

Commit Message

Eric Biggers Dec. 30, 2016, 11:42 p.m. UTC
From: Eric Biggers <ebiggers@google.com>

->setattr() was recently implemented for socket files to sync the socket
inode's uid to the new 'sk_uid' member of struct sock.  It does this by
copying over the ia_uid member of struct iattr.  However, ia_uid is
actually only valid when ATTR_UID is set in ia_valid, indicating that
the uid is being changed, e.g. by chown.  Other metadata operations such
as chmod or utimes leave ia_uid uninitialized.  Therefore, sk_uid could
be set to a "garbage" value from the stack.

Fix this by only copying the uid over when ATTR_UID is set.

Fixes: 86741ec25462 ("net: core: Add a UID field to struct sock.")
Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 net/socket.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

David Miller Dec. 31, 2016, 5:36 p.m. UTC | #1
From: Eric Biggers <ebiggers3@gmail.com>
Date: Fri, 30 Dec 2016 17:42:32 -0600

> From: Eric Biggers <ebiggers@google.com>
> 
> ->setattr() was recently implemented for socket files to sync the socket
> inode's uid to the new 'sk_uid' member of struct sock.  It does this by
> copying over the ia_uid member of struct iattr.  However, ia_uid is
> actually only valid when ATTR_UID is set in ia_valid, indicating that
> the uid is being changed, e.g. by chown.  Other metadata operations such
> as chmod or utimes leave ia_uid uninitialized.  Therefore, sk_uid could
> be set to a "garbage" value from the stack.
> 
> Fix this by only copying the uid over when ATTR_UID is set.
> 
> Fixes: 86741ec25462 ("net: core: Add a UID field to struct sock.")
> Signed-off-by: Eric Biggers <ebiggers@google.com>

Lorenzo, please review.
Lorenzo Colitti Jan. 1, 2017, 7:57 a.m. UTC | #2
On Sat, Dec 31, 2016 at 8:42 AM, Eric Biggers <ebiggers3@gmail.com> wrote:
> ->setattr() was recently implemented for socket files to sync the socket
> inode's uid to the new 'sk_uid' member of struct sock.  It does this by
> copying over the ia_uid member of struct iattr.  However, ia_uid is
> actually only valid when ATTR_UID is set in ia_valid, indicating that
> the uid is being changed, e.g. by chown.
> [...]
> -       if (!err) {
> +       if (!err && (iattr->ia_valid & ATTR_UID)) {

Oops. Thanks for fixing this. Unit tested in
https://android-review.googlesource.com/316594 .

Tested-by: Lorenzo Colitti <lorenzo@google.com>
Acked-by: Lorenzo Colitti <lorenzo@google.com>
David Miller Jan. 1, 2017, 4:54 p.m. UTC | #3
From: Lorenzo Colitti <lorenzo@google.com>
Date: Sun, 1 Jan 2017 16:57:23 +0900

> On Sat, Dec 31, 2016 at 8:42 AM, Eric Biggers <ebiggers3@gmail.com> wrote:
>> ->setattr() was recently implemented for socket files to sync the socket
>> inode's uid to the new 'sk_uid' member of struct sock.  It does this by
>> copying over the ia_uid member of struct iattr.  However, ia_uid is
>> actually only valid when ATTR_UID is set in ia_valid, indicating that
>> the uid is being changed, e.g. by chown.
>> [...]
>> -       if (!err) {
>> +       if (!err && (iattr->ia_valid & ATTR_UID)) {
> 
> Oops. Thanks for fixing this. Unit tested in
> https://android-review.googlesource.com/316594 .
> 
> Tested-by: Lorenzo Colitti <lorenzo@google.com>
> Acked-by: Lorenzo Colitti <lorenzo@google.com>

Applied, thanks everyone.
diff mbox

Patch

diff --git a/net/socket.c b/net/socket.c
index 8487bf136e5c..a8c2307590b8 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -537,7 +537,7 @@  int sockfs_setattr(struct dentry *dentry, struct iattr *iattr)
 {
 	int err = simple_setattr(dentry, iattr);
 
-	if (!err) {
+	if (!err && (iattr->ia_valid & ATTR_UID)) {
 		struct socket *sock = SOCKET_I(d_inode(dentry));
 
 		sock->sk->sk_uid = iattr->ia_uid;