Skip to contentSkip to conversation

sock filter: fix copy of filter from userspace

PATCH In review Stephen Hemminger 2 participants 3 messages Oct 13, 2012 – Oct 15, 2012

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sock filter: fix copy of filter from userspace

The sk_unattached_filter_create function is passed a socket filter structure and the copies the contents of the filter from userspace. Sparse detected that this code was incorrectly using memcpy when it needed to use copy_from_user instead.

The only use of sk_unattached_filter_create at present is in the team driver.

net/core/filter.c +3 -1
@@ -666,7 +666,9 @@ int sk_unattached_filter_create(struct s
 	fp = kmalloc(fsize + sizeof(*fp), GFP_KERNEL);
 	if (!fp)
 		return -ENOMEM;
-	memcpy(fp->insns, fprog->filter, fsize);
+
+	if (copy_from_user(fp->insns, fprog->filter, fsize))
+		return -EFAULT;
 
 	atomic_set(&fp->refcnt, 1);
 	fp->len = fprog->len;
#1 lore raw
Quoted 4 lines · Sat, Oct 13, 2012 at 08:40:32PM CEST, [email protected] wrote:

The sk_unattached_filter_create function is passed a socket filter structure and the copies the contents of the filter from userspace. Sparse detected that this code was incorrectly using memcpy when it needed to use copy_from_user instead.

Hmm. fprog->filter is in this case allocated and filled in kernel. So memcpy is good.

Not sure how to handle this correctly. Either we remove "__user" or we redefine "struct sock_fprog" for sk_unattached_filter_create() use.

Any thoughts?

Quoted 24 lines

The only use of sk_unattached_filter_create at present is in the team driver.

net/core/filter.c +3 -2
@@ -666,7 +666,9 @@ int sk_unattached_filter_create(struct s
 	fp = kmalloc(fsize + sizeof(*fp), GFP_KERNEL);
 	if (!fp)
 		return -ENOMEM;
-	memcpy(fp->insns, fprog->filter, fsize);
+
+	if (copy_from_user(fp->insns, fprog->filter, fsize))
+		return -EFAULT;
 
 	atomic_set(&fp->refcnt, 1);
 	fp->len = fprog->len;
--

To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw
Quoted 6 lines · On Sat, 13 Oct 2012 21:19:49 +0200 Jiri Pirko <[email protected]> wrote:
Quoted 4 lines · Sat, Oct 13, 2012 at 08:40:32PM CEST, [email protected] wrote:

The sk_unattached_filter_create function is passed a socket filter structure and the copies the contents of the filter from userspace. Sparse detected that this code was incorrectly using memcpy when it needed to use copy_from_user instead.

Quoted 7 lines

Hmm. fprog->filter is in this case allocated and filled in kernel. So memcpy is good.

Not sure how to handle this correctly. Either we remove "__user" or we redefine "struct sock_fprog" for sk_unattached_filter_create() use.

Any thoughts?

Or define a different structure for kernel usage versus userspace API.