mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Greg KH <gregkh@suse.de>
To: linux-kernel@vger.kernel.org, stable@kernel.org
Cc: stable-review@kernel.org, torvalds@linux-foundation.org,
	akpm@linux-foundation.org, alan@lxorguk.ukuu.org.uk,
	Eric Dumazet <eric.dumazet@gmail.com>,
	"David S. Miller" <davem@davemloft.net>
Subject: [07/68] filter: make sure filters dont read uninitialized memory
Date: Mon, 28 Feb 2011 08:22:28 -0800	[thread overview]
Message-ID: <20110228162406.369267453@clark.kroah.org> (raw)
In-Reply-To: <20110228163502.GA27221@kroah.com>

2.6.32-longterm review patch.  If anyone has any objections, please let us know.

------------------

From: David S. Miller <davem@davemloft.net>

commit 57fe93b374a6b8711995c2d466c502af9f3a08bb upstream.

There is a possibility malicious users can get limited information about
uninitialized stack mem array. Even if sk_run_filter() result is bound
to packet length (0 .. 65535), we could imagine this can be used by
hostile user.

Initializing mem[] array, like Dan Rosenberg suggested in his patch is
expensive since most filters dont even use this array.

Its hard to make the filter validation in sk_chk_filter(), because of
the jumps. This might be done later.

In this patch, I use a bitmap (a single long var) so that only filters
using mem[] loads/stores pay the price of added security checks.

For other filters, additional cost is a single instruction.

[ Since we access fentry->k a lot now, cache it in a local variable
  and mark filter entry pointer as const. -DaveM ]

Reported-by: Dan Rosenberg <drosenberg@vsecurity.com>
Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
[Backported by dann frazier <dannf@debian.org>]
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

---
 net/core/filter.c |   64 +++++++++++++++++++++++++++++-------------------------
 1 file changed, 35 insertions(+), 29 deletions(-)

--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -111,39 +111,41 @@ EXPORT_SYMBOL(sk_filter);
  */
 unsigned int sk_run_filter(struct sk_buff *skb, struct sock_filter *filter, int flen)
 {
-	struct sock_filter *fentry;	/* We walk down these */
 	void *ptr;
 	u32 A = 0;			/* Accumulator */
 	u32 X = 0;			/* Index Register */
 	u32 mem[BPF_MEMWORDS];		/* Scratch Memory Store */
+	unsigned long memvalid = 0;
 	u32 tmp;
 	int k;
 	int pc;
 
+	BUILD_BUG_ON(BPF_MEMWORDS > BITS_PER_LONG);
 	/*
 	 * Process array of filter instructions.
 	 */
 	for (pc = 0; pc < flen; pc++) {
-		fentry = &filter[pc];
+		const struct sock_filter *fentry = &filter[pc];
+		u32 f_k = fentry->k;
 
 		switch (fentry->code) {
 		case BPF_ALU|BPF_ADD|BPF_X:
 			A += X;
 			continue;
 		case BPF_ALU|BPF_ADD|BPF_K:
-			A += fentry->k;
+			A += f_k;
 			continue;
 		case BPF_ALU|BPF_SUB|BPF_X:
 			A -= X;
 			continue;
 		case BPF_ALU|BPF_SUB|BPF_K:
-			A -= fentry->k;
+			A -= f_k;
 			continue;
 		case BPF_ALU|BPF_MUL|BPF_X:
 			A *= X;
 			continue;
 		case BPF_ALU|BPF_MUL|BPF_K:
-			A *= fentry->k;
+			A *= f_k;
 			continue;
 		case BPF_ALU|BPF_DIV|BPF_X:
 			if (X == 0)
@@ -151,49 +153,49 @@ unsigned int sk_run_filter(struct sk_buf
 			A /= X;
 			continue;
 		case BPF_ALU|BPF_DIV|BPF_K:
-			A /= fentry->k;
+			A /= f_k;
 			continue;
 		case BPF_ALU|BPF_AND|BPF_X:
 			A &= X;
 			continue;
 		case BPF_ALU|BPF_AND|BPF_K:
-			A &= fentry->k;
+			A &= f_k;
 			continue;
 		case BPF_ALU|BPF_OR|BPF_X:
 			A |= X;
 			continue;
 		case BPF_ALU|BPF_OR|BPF_K:
-			A |= fentry->k;
+			A |= f_k;
 			continue;
 		case BPF_ALU|BPF_LSH|BPF_X:
 			A <<= X;
 			continue;
 		case BPF_ALU|BPF_LSH|BPF_K:
-			A <<= fentry->k;
+			A <<= f_k;
 			continue;
 		case BPF_ALU|BPF_RSH|BPF_X:
 			A >>= X;
 			continue;
 		case BPF_ALU|BPF_RSH|BPF_K:
-			A >>= fentry->k;
+			A >>= f_k;
 			continue;
 		case BPF_ALU|BPF_NEG:
 			A = -A;
 			continue;
 		case BPF_JMP|BPF_JA:
-			pc += fentry->k;
+			pc += f_k;
 			continue;
 		case BPF_JMP|BPF_JGT|BPF_K:
-			pc += (A > fentry->k) ? fentry->jt : fentry->jf;
+			pc += (A > f_k) ? fentry->jt : fentry->jf;
 			continue;
 		case BPF_JMP|BPF_JGE|BPF_K:
-			pc += (A >= fentry->k) ? fentry->jt : fentry->jf;
+			pc += (A >= f_k) ? fentry->jt : fentry->jf;
 			continue;
 		case BPF_JMP|BPF_JEQ|BPF_K:
-			pc += (A == fentry->k) ? fentry->jt : fentry->jf;
+			pc += (A == f_k) ? fentry->jt : fentry->jf;
 			continue;
 		case BPF_JMP|BPF_JSET|BPF_K:
-			pc += (A & fentry->k) ? fentry->jt : fentry->jf;
+			pc += (A & f_k) ? fentry->jt : fentry->jf;
 			continue;
 		case BPF_JMP|BPF_JGT|BPF_X:
 			pc += (A > X) ? fentry->jt : fentry->jf;
@@ -208,7 +210,7 @@ unsigned int sk_run_filter(struct sk_buf
 			pc += (A & X) ? fentry->jt : fentry->jf;
 			continue;
 		case BPF_LD|BPF_W|BPF_ABS:
-			k = fentry->k;
+			k = f_k;
 load_w:
 			ptr = load_pointer(skb, k, 4, &tmp);
 			if (ptr != NULL) {
@@ -217,7 +219,7 @@ load_w:
 			}
 			break;
 		case BPF_LD|BPF_H|BPF_ABS:
-			k = fentry->k;
+			k = f_k;
 load_h:
 			ptr = load_pointer(skb, k, 2, &tmp);
 			if (ptr != NULL) {
@@ -226,7 +228,7 @@ load_h:
 			}
 			break;
 		case BPF_LD|BPF_B|BPF_ABS:
-			k = fentry->k;
+			k = f_k;
 load_b:
 			ptr = load_pointer(skb, k, 1, &tmp);
 			if (ptr != NULL) {
@@ -241,32 +243,34 @@ load_b:
 			X = skb->len;
 			continue;
 		case BPF_LD|BPF_W|BPF_IND:
-			k = X + fentry->k;
+			k = X + f_k;
 			goto load_w;
 		case BPF_LD|BPF_H|BPF_IND:
-			k = X + fentry->k;
+			k = X + f_k;
 			goto load_h;
 		case BPF_LD|BPF_B|BPF_IND:
-			k = X + fentry->k;
+			k = X + f_k;
 			goto load_b;
 		case BPF_LDX|BPF_B|BPF_MSH:
-			ptr = load_pointer(skb, fentry->k, 1, &tmp);
+			ptr = load_pointer(skb, f_k, 1, &tmp);
 			if (ptr != NULL) {
 				X = (*(u8 *)ptr & 0xf) << 2;
 				continue;
 			}
 			return 0;
 		case BPF_LD|BPF_IMM:
-			A = fentry->k;
+			A = f_k;
 			continue;
 		case BPF_LDX|BPF_IMM:
-			X = fentry->k;
+			X = f_k;
 			continue;
 		case BPF_LD|BPF_MEM:
-			A = mem[fentry->k];
+			A = (memvalid & (1UL << f_k)) ?
+				mem[f_k] : 0;
 			continue;
 		case BPF_LDX|BPF_MEM:
-			X = mem[fentry->k];
+			X = (memvalid & (1UL << f_k)) ?
+				mem[f_k] : 0;
 			continue;
 		case BPF_MISC|BPF_TAX:
 			X = A;
@@ -275,14 +279,16 @@ load_b:
 			A = X;
 			continue;
 		case BPF_RET|BPF_K:
-			return fentry->k;
+			return f_k;
 		case BPF_RET|BPF_A:
 			return A;
 		case BPF_ST:
-			mem[fentry->k] = A;
+			memvalid |= 1UL << f_k;
+			mem[f_k] = A;
 			continue;
 		case BPF_STX:
-			mem[fentry->k] = X;
+			memvalid |= 1UL << f_k;
+			mem[f_k] = X;
 			continue;
 		default:
 			WARN_ON(1);



  parent reply	other threads:[~2011-02-28 16:52 UTC|newest]

Thread overview: 72+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-02-28 16:35 [00/68] 2.6.32.30-longterm review Greg KH
2011-02-28 16:22 ` [01/68] NFSD: memory corruption due to writing beyond the stat array Greg KH
2011-02-28 16:22 ` [02/68] [SCSI] mptfusion: mptctl_release is required in mptctl.c Greg KH
2011-02-28 16:22 ` [03/68] [SCSI] mptfusion: Fix Incorrect return value in mptscsih_dev_reset Greg KH
2011-02-28 16:22 ` [04/68] sctp: Fix out-of-bounds reading in sctp_asoc_get_hmac() Greg KH
2011-02-28 16:22 ` [05/68] ocfs2_connection_find() returns pointer to bad structure Greg KH
2011-02-28 16:22 ` [06/68] Fix pktcdvd ioctl dev_minor range check Greg KH
2011-02-28 16:22 ` Greg KH [this message]
2011-02-28 16:22 ` [08/68] x25: decrement netdev reference counts on unload Greg KH
2011-02-28 16:22 ` [09/68] x86, hpet: Disable per-cpu hpet timer if ARAT is supported Greg KH
2011-02-28 16:22 ` [10/68] OHCI: work around for nVidia shutdown problem Greg KH
2011-02-28 16:22 ` [11/68] x86/pvclock: Zero last_value on resume Greg KH
2011-02-28 16:22 ` [12/68] [media] [v3,media] av7110: check for negative array offset Greg KH
2011-02-28 16:22 ` [13/68] CRED: Fix get_task_cred() and task_state() to not resurrect dead credentials Greg KH
2011-02-28 16:22 ` [14/68] bonding/vlan: Avoid mangled NAs on slaves without VLAN tag insertion Greg KH
2011-02-28 16:22 ` [15/68] CRED: Fix kernel panic upon security_file_alloc() failure Greg KH
2011-02-28 16:22 ` [16/68] CRED: Fix BUG() upon security_cred_alloc_blank() failure Greg KH
2011-02-28 16:22 ` [17/68] CRED: Fix memory and refcount leaks upon security_prepare_creds() failure Greg KH
2011-02-28 16:22 ` [18/68] sendfile(): check f_op.splice_write() rather than f_op.sendpage() Greg KH
2011-02-28 16:22 ` [19/68] NFS: fix the return value of nfs_file_fsync() Greg KH
2011-02-28 16:22 ` [20/68] isdn: hisax: Replace the bogus access to irq stats Greg KH
2011-02-28 16:22 ` [21/68] ixgbe: add support for 82599 based Express Module X520-P2 Greg KH
2011-02-28 16:22 ` [22/68] ixgbe: prevent speculative processing of descriptors before ready Greg KH
2011-03-01  2:14   ` [Stable-review] " Ben Hutchings
2011-03-01 20:46     ` Greg KH
2011-03-01 21:56       ` Jeff Kirsher
2011-02-28 16:22 ` [23/68] [SCSI] scsi_dh_alua: add netapp to dev list Greg KH
2011-02-28 16:22 ` [24/68] [SCSI] scsi_dh_alua: Add IBM Power Virtual SCSI ALUA device " Greg KH
2011-02-28 16:22 ` [25/68] dm raid1: fail writes if errors are not handled and log fails Greg KH
2011-02-28 16:22 ` [26/68] GFS2: Fix bmap allocation corner-case bug Greg KH
2011-02-28 16:22 ` [27/68] dm raid1: fix null pointer dereference in suspend Greg KH
2011-02-28 16:22 ` [28/68] sunrpc/cache: fix module refcnt leak in a failure path Greg KH
2011-02-28 16:22 ` [29/68] be2net: Maintain tx and rx counters in driver Greg KH
2011-02-28 16:22 ` [30/68] tcp: Increase TCP_MAXSEG socket option minimum Greg KH
2011-02-28 16:22 ` [31/68] tcp: Make TCP_MAXSEG minimum more correct Greg KH
2011-02-28 16:22 ` [32/68] nfsd: correctly handle return value from nfsd_map_name_to_* Greg KH
2011-02-28 16:22 ` [33/68] xfs: always use iget in bulkstat Greg KH
2011-02-28 16:22 ` [34/68] xfs: validate untrusted inode numbers during lookup Greg KH
2011-02-28 16:22 ` [35/68] xfs: rename XFS_IGET_BULKSTAT to XFS_IGET_UNTRUSTED Greg KH
2011-02-28 16:22 ` [36/68] xfs: remove block number from inode lookup code Greg KH
2011-02-28 16:22 ` [37/68] xfs: fix untrusted inode number lookup Greg KH
2011-02-28 16:22 ` [38/68] s390: remove task_show_regs Greg KH
2011-02-28 16:23 ` [39/68] PM / Hibernate: Return error code when alloc_image_page() fails Greg KH
2011-02-28 16:23 ` [40/68] fs/partitions: Validate map_count in Mac partition tables Greg KH
2011-02-28 16:23 ` [41/68] ALSA: HDA: Add position_fix quirk for an Asus device Greg KH
2011-02-28 16:23 ` [42/68] ALSA: caiaq - Fix possible string-buffer overflow Greg KH
2011-02-28 16:23 ` [43/68] [media] radio-aimslab.c needs #include <linux/delay.h> Greg KH
2011-02-28 16:23 ` [44/68] ARM: Ensure predictable endian state on signal handler entry Greg KH
2011-02-28 16:23 ` [45/68] acer-wmi: Fix capitalisation of GUID Greg KH
2011-02-28 16:23 ` [46/68] eCryptfs: Copy up lower inode attrs in getattr Greg KH
2011-02-28 16:23 ` [47/68] platform: x86: acer-wmi: world-writable sysfs threeg file Greg KH
2011-02-28 16:23 ` [48/68] platform: x86: asus_acpi: world-writable procfs files Greg KH
2011-02-28 16:23 ` [49/68] platform: x86: tc1100-wmi: world-writable sysfs wireless and jogdial files Greg KH
2011-02-28 16:23 ` [50/68] genirq: Disable the SHIRQ_DEBUG call in request_threaded_irq for now Greg KH
2011-02-28 16:23 ` [51/68] usb: musb: omap2430: fix kernel panic on reboot Greg KH
2011-02-28 16:23 ` [52/68] USB: add quirks entry for Keytouch QWERTY Panel Greg KH
2011-02-28 16:23 ` [53/68] USB: Add Samsung SGH-I500/Android modem ID switch to visor driver Greg KH
2011-02-28 16:23 ` [54/68] USB: Add quirk for Samsung Android phone modem Greg KH
2011-02-28 16:23 ` [55/68] p54pci: update receive dma buffers before and after processing Greg KH
2011-02-28 16:23 ` [56/68] sierra: add new ID for Airprime/Sierra USB IP modem Greg KH
2011-02-28 16:23 ` [57/68] staging: usbip: vhci: update reference count for usb_device Greg KH
2011-02-28 16:23 ` [58/68] staging: usbip: vhci: give back URBs from in-flight unlink requests Greg KH
2011-02-28 16:23 ` [59/68] staging: usbip: vhci: refuse to enqueue for dead connections Greg KH
2011-02-28 16:23 ` [60/68] staging: usbip: vhci: use urb->dev->portnum to find port Greg KH
2011-02-28 16:23 ` [61/68] epoll: prevent creating circular epoll structures Greg KH
2011-02-28 16:23 ` [62/68] ldm: corrupted partition table can cause kernel oops Greg KH
2011-02-28 16:23 ` [63/68] md: correctly handle probe of an mdp device Greg KH
2011-02-28 16:23 ` [64/68] x86 quirk: Fix polarity for IRQ0 pin2 override on SB800 systems Greg KH
2011-02-28 16:23 ` [65/68] xhci: Avoid BUG() in interrupt context Greg KH
2011-02-28 16:23 ` [66/68] xhci: Clarify some expressions in the TRB math Greg KH
2011-02-28 16:23 ` [67/68] xhci: Fix errors in the running total calculations " Greg KH
2011-02-28 16:23 ` [68/68] xhci: Fix an error in count_sg_trbs_needed() Greg KH

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20110228162406.369267453@clark.kroah.org \
    --to=gregkh@suse.de \
    --cc=akpm@linux-foundation.org \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=davem@davemloft.net \
    --cc=eric.dumazet@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable-review@kernel.org \
    --cc=stable@kernel.org \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®