Skip to contentSkip to conversation

Various static checker fixes

PATCH Applied Andi Kleen 13 participants 34 messages Sep 30, 2013 – Apr 9, 2014

Subsystems (11)
lore
Share & tools
lore

Andi Kleen

author
#0 lore raw

I was playing around with LLVM's static checker. Here are some fixes from reviewing the output on a kernel build.

Some merely shut up the checker -- it is technically right, but the problem is not a real bug as far as I can tell. Still it's cleaner/clearer to avoid uninitialized variables and similar.

A few others are real bugs.

-Andi

author
#1 lore raw

PATCH 1/11 random: don't feed stack data into pool when interrupt regs NULL

From: Andi Kleen <[email protected]>

In some cases, e.g. after this

arch/ia64/kernel/irq.c:185: struct pt_regs *old_regs = set_irq_regs(NULL);
arch/ia64/kernel/irq_ia64.c:560: struct pt_regs *old_regs = set_irq_regs(NULL);

the regs passed to add_interrupt_randomness() could be NULL.

In this case fast_mix would use two uninitialized ints from the stack and mix it into the pool.

In this case set the input to 0.

---
 drivers/char/random.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/char/random.c +2 -0
index 7737b5b..25ed2dc 100644
@@ -752,6 +752,8 @@ void add_interrupt_randomness(int irq, int irq_flags)
 		__u64 ip = instruction_pointer(regs);
 		input[2] = ip;
 		input[3] = ip >> 32;
+	} else {
+		input[2] = input[3] = 0;
 	}
 
 	fast_mix(fast_pool, input, sizeof(input));
Signature · 1 line
1.8.3.1
#2 lore raw

RE: [PATCH 01/11] random: don't feed stack data into pool when interrupt regs NULL

Quoted 2 lines

In this case fast_mix would use two uninitialized ints from the stack and mix it into the pool.

Is the concern here is that an attacker might know (or be able to control) what is on the stack - and so get knowledge of what is being mixed into the pool?

Quoted 1 line

In this case set the input to 0.

And the fix is to guarantee that everyone knows what is being mixed in? (!)

Wouldn't it be better to adjust the "nbytes" parameter to

	fast_mix(..., ..., sizeof (input));

to only mix in the part of input[] that we successfully initialized?

Untested patch below.

---

drivers/char/random.c +4 -1
index 7737b5bd26af..5c4ec0abb702 100644
@@ -745,16 +745,19 @@ void add_interrupt_randomness(int irq, int irq_flags)
 	struct pt_regs		*regs = get_irq_regs();
 	unsigned long		now = jiffies;
 	__u32			input[4], cycles = get_cycles();
+	int			nbytes;
 
 	input[0] = cycles ^ jiffies;
 	input[1] = irq;
+	nbytes = 2 * sizeof(input[0]);
 	if (regs) {
 		__u64 ip = instruction_pointer(regs);
 		input[2] = ip;
 		input[3] = ip >> 32;
+		nbytes += 2 * sizeof(input[0]);
 	}
 
-	fast_mix(fast_pool, input, sizeof(input));
+	fast_mix(fast_pool, input, nbytes);
 
 	if ((fast_pool->count & 1023) &&
 	    !time_after(now, fast_pool->last + HZ))
maintainer
#3 lore raw

Re: [PATCH 01/11] random: don't feed stack data into pool when interrupt regs NULL

Quoted 5 lines · Tony
Quoted 2 lines

In this case fast_mix would use two uninitialized ints from the stack and mix it into the pool.

Is the concern here is that an attacker might know (or be able to control) what is on the stack - and so get knowledge of what is being mixed into the pool?

Yes, this is a bogus complaint.

Quoted 9 lines
Quoted 1 line

In this case set the input to 0.

And the fix is to guarantee that everyone knows what is being mixed in? (!)

Wouldn't it be better to adjust the "nbytes" parameter to

	fast_mix(..., ..., sizeof (input));

to only mix in the part of input[] that we successfully initialized?

The changes queued for the next merge window in the random tree solve this problem slightly differently:

	...
	input[0] = cycles ^ j_high ^ irq;
	input[1] = now ^ c_high;
	ip = regs ? instruction_pointer(regs) : _RET_IP_;
	input[2] = ip;
	input[3] = ip >> 32;

	fast_mix(fast_pool, input);
	...

(Note the lack of nbytes parameter in fast_mix --- there are some optimizations so that we mix in the changes by 32-bit words, instead of bytes, and the number of 32-bit words is fixed to 4, since it's the only way fast_mix is called).

_RET_IP_ isn't that much better than 0, but it's at least kernel specific, and I figured it was better to shut up bogus warnings, as opposed to trying to depend on stack garbage (which would likely be kernel specific anyway).

Cheers,

- Ted

#4 lore raw

Re: [PATCH 01/11] random: don't feed stack data into pool when interrupt regs NULL

Quoted 18 lines · Theodore Ts'o

The changes queued for the next merge window in the random tree solve this problem slightly differently:

	...
	input[0] = cycles ^ j_high ^ irq;
	input[1] = now ^ c_high;
	ip = regs ? instruction_pointer(regs) : _RET_IP_;
	input[2] = ip;
	input[3] = ip >> 32;

	fast_mix(fast_pool, input);
	...

(Note the lack of nbytes parameter in fast_mix --- there are some optimizations so that we mix in the changes by 32-bit words, instead of bytes, and the number of 32-bit words is fixed to 4, since it's the only way fast_mix is called).

Quoted 4 lines

_RET_IP_ isn't that much better than 0, but it's at least kernel specific, and I figured it was better to shut up bogus warnings, as opposed to trying to depend on stack garbage (which would likely be kernel specific anyway).

That ip pointer was earlier optional. Now with _RET_IP_ it is a constant since there is _one_ caller. Plus on 32bit the upper bits are always zero. It probably didn't get worse because the four bytes on stack were mostlikly constant as well. [2] is constant if _RET_IP_ is used. The IP is kind of random. The lower bits are mostlikely 0 due to 32bit alignment (not on x86, okay).
Lets look further. c_high is != 0 only if cycles is larger than 4 bytes. This is in most cases a long which makes 4 bytes on all 32bit arches. This makes [1] the lower bytes of jiffies. And you can imagine how often the upper 16bit change.
Which brings me to [0]. The irq number changes now and then and mostlikely only the lower 8 bit. j_high is 0 on 32bit platforms. Even on 64bit with HZ=250 the lower 32bit overflows every ~198 days unless I miscalculated. Doesn't this make it a constant?
And finally cycles which is random_get_entropy(). On ARM (as previously on MIPS) this returns 0. Well not always but for all platforms which do not implement register_current_timer_delay() which makes a lot of them. This makes
[0] = irq (8 bit)
[1] = jiffies
[2] = a constant unless regs is available and
[3] = 0

from looking at the code it reads like 16 random bytes are fed but now it looks a little different.

May I kill this and save a few cycles in irq context?
Why don't you take a small amount of randomness and use that as a key and feed into a block cipher in CTR mode instead of giving it to user right away? This _can_ work in parallell and should provide *good* pseudo random numbers on demand with a high performance.

But seriously. What about this:

Quoted 1 line

From 082dab5c482728a9ef695aa5b42217dcec8e3dd5 Mon Sep 17 00:00:00 2001

From: Sebastian Andrzej Siewior <[email protected]>
Date: Fri, 4 Apr 2014 18:49:29 +0200
Subject: [PATCH] random: yell if random_get_entropy() returns a constant

A few architectures still return 0 (i.e. a constant) if random_get_entropy() is invoked. Make them aware of this so they may fix this.

---
 drivers/char/random.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
drivers/char/random.c +21 -0
index 429b75b..48775f8 100644
@@ -241,6 +241,7 @@
 #include <linux/major.h>
 #include <linux/string.h>
 #include <linux/fcntl.h>
+#include <linux/delay.h>
 #include <linux/slab.h>
 #include <linux/random.h>
 #include <linux/poll.h>
@@ -1675,3 +1676,23 @@ randomize_range(unsigned long start, unsigned long end, unsigned long len)
 		return 0;
 	return PAGE_ALIGN(get_random_int() % range + start);
 }
+
+static int check_random_get_entropy(void)
+{
+	cycles_t cyc1;
+	cycles_t cyc2;
+
+	cyc1 = random_get_entropy();
+	cyc2 = random_get_entropy();
+	if (cyc1 != cyc2)
+		return 0;
+	udelay(1);
+	cyc2 = random_get_entropy();
+
+	if (cyc1 != cyc2)
+		return 0;
+	pr_err("Error: random_get_entropy() does not return anything random\n");
+	WARN_ON(1);
+	return -EINVAL;
+}
+late_initcall(check_random_get_entropy);
Signature · 1 line
1.9.1
Quoted 2 lines

Cheers,
- Ted

Sebastian

maintainer
#5 lore raw

Re: [PATCH 01/11] random: don't feed stack data into pool when interrupt regs NULL

Quoted 3 lines · Sebastian Andrzej Siewior

And finally cycles which is random_get_entropy(). On ARM (as previously on MIPS) this returns 0. Well not always but for all platforms which do not implement register_current_timer_delay() which makes a lot of them.

Yes, ARM sucks for not implement random_get_entropy() on all platforms. Film at 11. I'm told that Cortex ARM systms are supposed to have a cycle counter. I'm not sure why it hasn't been wired up, but it really should be.

Quoted 4 lines

[0] = irq (8 bit)
[1] = jiffies
[2] = a constant unless regs is available and
[3] = 0

Actually regs should be available nearly all of the time. So the situation isn't quite as dire as you describe, but agreed, it is pretty bad.

Still, it would be nice if random_get_entropy() could be wired up on as many platforms as possible.

Quoted 9 lines
drivers/char/random.c +9 -0
+static int check_random_get_entropy(void)
+{
+	cycles_t cyc1;
+	cycles_t cyc2;
+
+	cyc1 = random_get_entropy();
+	cyc2 = random_get_entropy();
+	if (cyc1 != cyc2)
+		return 0;

drivers/char/random.c randomize_range(unsigned long start, unsigned long end, unsigned long len) · line 1698

Quoted 10 lines
drivers/char/random.c +10 -0
+	udelay(1);
+	cyc2 = random_get_entropy();
+
+	if (cyc1 != cyc2)
+		return 0;
+	pr_err("Error: random_get_entropy() does not return anything random\n");
+	WARN_ON(1);
+	return -EINVAL;
+}
+late_initcall(check_random_get_entropy);

This is lacking in subtly, and while I'm sympathetic with your frustration, and unfortunately, there are a huge number of CPU's/SOC's that don't implement a cycle counter or some other kind of cheap, high resolution counter. I'm not against putting in a warning printk, but issuing a WARN_ON(1) would be really be too obnoxious. Maybe something like, "you're running on a crap CPU architecture and so /dev/random may very well be insecure", but I think that's about as blunt as we can really afford to be at this point.

- Ted

P.S. Maybe if Intel started a marketing campaign on G+ explaining why ChromeOS on Intel machines is much more secure than ChromeOS on ARM, we could finally get some action out of the !@#!?! ARM chip manufacturers. :-/

#6 lore raw

Re: [PATCH 01/11] random: don't feed stack data into pool when interrupt regs NULL

Quoted 4 lines · Theodore Ts'o

Yes, ARM sucks for not implement random_get_entropy() on all platforms. Film at 11. I'm told that Cortex ARM systms are supposed to have a cycle counter. I'm not sure why it hasn't been wired up, but it really should be.

I see.

Quoted 3 lines

Actually regs should be available nearly all of the time. So the situation isn't quite as dire as you describe, but agreed, it is pretty bad.

You dropped that part where I suggested to use something like AES+CTR and create the numbers on demand and dropping that attempt to create as much random data with custom functions as possible. You completly dislike that approach? And if so, why?

Quoted 2 lines

Still, it would be nice if random_get_entropy() could be wired up on as many platforms as possible.

Yes. Usually there is generic function doing something sane but not as good as it could do with arch specific code. Or the code is completly disabled unless the architecture wires it up. Dropping a new function and hoping everyone will wire it up in no time is, ehm, brave. Nobody implemented random_get_entropy(), everyone falls back to get_cycles. From a quick grep I can see that atleast Hexagon, Cris, Frv, m32r and m68k return 0. I put some of the maintainers Cc, I am curious if they know about the side effects.

Quoted 4 lines

This is lacking in subtly, and while I'm sympathetic with your frustration, and unfortunately, there are a huge number of CPU's/SOC's that don't implement a cycle counter or some other kind of cheap, high resolution counter. I'm not against putting in a warning printk, but

Cheap? I know there a few platforms running on a 16bit counter. It is enough for a high resolution timer, it overflows quite often, too (i.e. in less than two seconds). However it is the only thing as time base that they have and from clocksource point of view it is enough.

Quoted 4 lines

issuing a WARN_ON(1) would be really be too obnoxious. Maybe something like, "you're running on a crap CPU architecture and so /dev/random may very well be insecure", but I think that's about as blunt as we can really afford to be at this point.

A backtrace is something you catch during boot up. A single line may get lost in the mass and I think this is important.
However, as you wish, here are the changes.

Quoted 7 lines

- Ted

P.S. Maybe if Intel started a marketing campaign on G+ explaining why ChromeOS on Intel machines is much more secure than ChromeOS on ARM, we could finally get some action out of the !@#!?! ARM chip manufacturers. :-/

Maybe post people are not aware what is going on here. A huge campaing with ballons is one way to get attention. Just drop /dev/random if the missing infrastucture isn't available and it shouldn't need G+ to get the infrastrucure. And the CTR thingy might be a good one because it would require *one* thing to get done and not each arch/subarch.

Quoted 1 line

From 1d2d3ef3c411af7fc6b1beb0db7a2c35c1a72702 Mon Sep 17 00:00:00 2001

From: Sebastian Andrzej Siewior <[email protected]>
Date: Fri, 4 Apr 2014 18:49:29 +0200
Subject: [PATCH] random: yell if random_get_entropy() returns a constant

A few architectures still return 0 (i.e. a constant) if random_get_entropy() is invoked. Make them aware of this so they may fix this.

---
 drivers/char/random.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
drivers/char/random.c +20 -0
index 429b75b..33508f9 100644
@@ -241,6 +241,7 @@
 #include <linux/major.h>
 #include <linux/string.h>
 #include <linux/fcntl.h>
+#include <linux/delay.h>
 #include <linux/slab.h>
 #include <linux/random.h>
 #include <linux/poll.h>
@@ -1675,3 +1676,22 @@ randomize_range(unsigned long start, unsigned long end, unsigned long len)
 		return 0;
 	return PAGE_ALIGN(get_random_int() % range + start);
 }
+
+static int check_random_get_entropy(void)
+{
+	cycles_t cyc1;
+	cycles_t cyc2;
+
+	cyc1 = random_get_entropy();
+	cyc2 = random_get_entropy();
+	if (cyc1 != cyc2)
+		return 0;
+	udelay(1);
+	cyc2 = random_get_entropy();
+	if (cyc1 != cyc2)
+		return 0;
+
+	pr_err("Error: you're running on a crap CPU architecture and so /dev/random may very well be insecure");
+	return -EINVAL;
+}
+late_initcall(check_random_get_entropy);
Signature · 1 line
1.9.1
maintainer
#7 lore raw

Re: [PATCH 01/11] random: don't feed stack data into pool when interrupt regs NULL

Quoted 5 lines · Sebastian Andrzej Siewior

You dropped that part where I suggested to use something like AES+CTR and create the numbers on demand and dropping that attempt to create as much random data with custom functions as possible. You completly dislike that approach? And if so, why?

Where are you going to get the "few random bits" from? Which crypto primitive you use and how you gather the entropy are two completely orothognal issue. If we can get at least 128 bits of secure randomness before the embedded platform trying to generate RSA private keys or otherwise depending on the RNG, we're fine. This is true regardless of whether we use the current /dev/random machinery or AES+CTR.

The reason why we are grabbing lots of bits from the interrupt handler is that we're hoping that *some* of them will not be guessable by the attacker. If we knew which ones were random, we wouldn't have to do this, yes. But that's like say, "playing the stock market is easy; all you have to do is buy low and sell high!"

Quoted 8 lines

Yes. Usually there is generic function doing something sane but not as good as it could do with arch specific code. Or the code is completly disabled unless the architecture wires it up. Dropping a new function and hoping everyone will wire it up in no time is, ehm, brave. Nobody implemented random_get_entropy(), everyone falls back to get_cycles. From a quick grep I can see that atleast Hexagon, Cris, Frv, m32r and m68k return 0. I put some of the maintainers Cc, I am curious if they know about the side effects.

What we have right now is now worse than what we had before. We introduced random_get_entryop() done because MIPS had a register which wouldn't qualify for get_cycles(), but was good enough for what the random driver had, so it allowed MIPS to be able to do a better job. Basically, I had a MIPS developer who was highly motiviated to improve security for home routers (which typically us MIPS), and I worked with him.

If there is some ARM developer who is interested in woring with me, that's great. I would love to have that. I've reached out to a few people in Linaro about this over the past couple of months, but nothing has happened yet.

- Ted

#8 lore raw

Re: [PATCH 01/11] random: don't feed stack data into pool when interrupt regs NULL

In reply to Sebastian Andrzej Siewior
Quoted 8 lines · Sebastian Andrzej Siewior

Yes. Usually there is generic function doing something sane but not as good as it could do with arch specific code. Or the code is completly disabled unless the architecture wires it up. Dropping a new function and hoping everyone will wire it up in no time is, ehm, brave. Nobody implemented random_get_entropy(), everyone falls back to get_cycles. From a quick grep I can see that atleast Hexagon, Cris, Frv, m32r and m68k return 0. I put some of the maintainers Cc, I am curious if they know about the side effects.

Thanks for the CC; I was not aware of the side effects. Hexagon does have a pcycles mechanism, so I will hook that up in our arch.

Signature · 3 lines
Sent by an employee of the Qualcomm Innovation Center, Inc.
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
hosted by The Linux Foundation
author
#9 lore raw

PATCH 2/11 Disable initialized_var for clang

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

LLVM clang doesn't understand uninitialized_var and always throws a warning. Disable the macro for this case.

---
 include/linux/compiler-gcc.h | 4 ++++
 1 file changed, 4 insertions(+)
include/linux/compiler-gcc.h +4 -0
index 24545cd..fa93722 100644
@@ -110,6 +110,10 @@
  * A trick to suppress uninitialized variable warning without generating any
  * code
  */
+#ifdef __clang__
+#define uninitialized_var(x) x
+#else
 #define uninitialized_var(x) x = x
+#endif
 
 #define __always_inline		inline __attribute__((always_inline))
Signature · 1 line
1.8.3.1
author
#10 lore raw

PATCH 3/11 posix-timers: Initialize timer value to 0 for invalid timers.

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

cpu_clock_sample et.al. can fail with an invalid timer type. For most callers this cannot happen because the timer has been successfully initialized earlier, so they don't check the return value, but still use the val output.

Unfortunately static checkers for uninitialied variables don't understand this and always throw a warning in this case.

Initialize the timer value to 0 for the error case to shut it up.

This doesn't fix any real bug, just reduces warning noise.

---
 kernel/posix-cpu-timers.c | 2 ++
 1 file changed, 2 insertions(+)
kernel/posix-cpu-timers.c +2 -0
index c7f31aa..cc3c3f2 100644
@@ -182,6 +182,7 @@ static int cpu_clock_sample(const clockid_t which_clock, struct task_struct *p,
 {
 	switch (CPUCLOCK_WHICH(which_clock)) {
 	default:
+		*sample = 0;
 		return -EINVAL;
 	case CPUCLOCK_PROF:
 		*sample = prof_ticks(p);
@@ -243,6 +244,7 @@ static int cpu_clock_sample_group(const clockid_t which_clock,
 
 	switch (CPUCLOCK_WHICH(which_clock)) {
 	default:
+		*sample = 0;
 		return -EINVAL;
 	case CPUCLOCK_PROF:
 		thread_group_cputime(p, &cputime);
Signature · 1 line
1.8.3.1
author
#11 lore raw

PATCH 4/11 block: Return error code of elevator init function

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

The error code was ignored, which I assume is a mistake.

---
 block/elevator.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
block/elevator.c +1 -3
index 2bcbd8c..6c2a8ee 100644
@@ -184,7 +184,6 @@ static void elevator_release(struct kobject *kobj)
 int elevator_init(struct request_queue *q, char *name)
 {
 	struct elevator_type *e = NULL;
-	int err;
 
 	if (unlikely(q->elevator))
 		return 0;
@@ -222,8 +221,7 @@ int elevator_init(struct request_queue *q, char *name)
 		}
 	}
 
-	err = e->ops.elevator_init_fn(q, e);
-	return 0;
+	return e->ops.elevator_init_fn(q, e);
 }
 EXPORT_SYMBOL(elevator_init);
 
Signature · 1 line
1.8.3.1
Reviewed-by on v1 4/11
#12 lore raw

Re: [PATCH 04/11] block: Return error code of elevator init function

Quoted 3 lines · Andi Kleen

From: Andi Kleen <[email protected]>

The error code was ignored, which I assume is a mistake.

Yeah, introduced in d50235b7bc3ee0a0427984d763ea7534149531b4, so I cc'd Jianpeng Ma, though it looks pretty obvious.

Quoted 27 lines
---
 block/elevator.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
block/elevator.c +1 -3
index 2bcbd8c..6c2a8ee 100644
@@ -184,7 +184,6 @@ static void elevator_release(struct kobject *kobj)
 int elevator_init(struct request_queue *q, char *name)
 {
 	struct elevator_type *e = NULL;
-	int err;
 
 	if (unlikely(q->elevator))
 		return 0;
@@ -222,8 +221,7 @@ int elevator_init(struct request_queue *q, char *name)
 		}
 	}
 
-	err = e->ops.elevator_init_fn(q, e);
-	return 0;
+	return e->ops.elevator_init_fn(q, e);
 }
 EXPORT_SYMBOL(elevator_init);
author
#13 lore raw

PATCH 5/11 seq_file: Handle ->next error in seq_read

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

When ->next fails the error would not be returned to user space. Add the missing check.

---
 fs/seq_file.c | 2 ++
 1 file changed, 2 insertions(+)
fs/seq_file.c +2 -0
index 3135c25..e16b4a8 100644
@@ -262,6 +262,8 @@ Fill:
 		pos = next;
 	}
 	m->op->stop(m, p);
+	if (err)
+		goto Done;
 	n = min(m->count, size);
 	err = copy_to_user(buf, m->buf, n);
 	if (err)
Signature · 1 line
1.8.3.1
author
#14 lore raw

PATCH 6/11 sysctl: remove unnecessary variable initialization

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

In this if () branch first is not needed, so no need to set it.

Fixes a set-but-not-used warning.

---
 kernel/sysctl.c | 1 -
 1 file changed, 1 deletion(-)
kernel/sysctl.c +0 -1
index b2f06f3..7f82d6d 100644
@@ -2549,7 +2549,6 @@ int proc_do_large_bitmap(struct ctl_table *table, int write,
 			}
 
 			bitmap_set(tmp_bitmap, val_a, val_b - val_a + 1);
-			first = 0;
 			proc_skip_char(&kbuf, &left, '\n');
 		}
 		free_page(page);
Signature · 1 line
1.8.3.1
author
#15 lore raw

PATCH 7/11 igb: Avoid uninitialized advertised variable in eee_set_cur

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

eee_get_cur assumes that the output data is already zeroed. It can read-modify-write the advertised field:

              if (ipcnfg & E1000_IPCNFG_EEE_100M_AN)

2594 edata->advertised |= ADVERTISED_100baseT_Full;

This is ok for the normal ethtool eee_get call, which always zeroes the input data before.

But eee_set_cur also calls eee_get_cur and it did not zero the input field. Later on it then compares agsinst the field, which can contain partial stack garbage.

Zero the input field in eee_set_cur() too.

---
 drivers/net/ethernet/intel/igb/igb_ethtool.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/net/ethernet/intel/igb/igb_ethtool.c +2 -0
index 48cbc83..41e37ff 100644
@@ -2652,6 +2652,8 @@ static int igb_set_eee(struct net_device *netdev,
 	    (hw->phy.media_type != e1000_media_type_copper))
 		return -EOPNOTSUPP;
 
+	memset(&eee_curr, 0, sizeof(struct ethtool_eee));
+
 	ret_val = igb_get_eee(netdev, &eee_curr);
 	if (ret_val)
 		return ret_val;
Signature · 1 line
1.8.3.1
#16 lore raw

RE: [PATCH 07/11] igb: Avoid uninitialized advertised variable in eee_set_cur

Quoted 47 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Andi Kleen
Sent: Monday, September 30, 2013 1:29 PM
To: [email protected]
Cc: Andi Kleen; Kirsher, Jeffrey T; [email protected]
Subject: [PATCH 07/11] igb: Avoid uninitialized advertised variable in eee_set_cur

From: Andi Kleen <[email protected]>

eee_get_cur assumes that the output data is already zeroed. It can read-modify- write the advertised field:

              if (ipcnfg & E1000_IPCNFG_EEE_100M_AN)

2594 edata->advertised |= ADVERTISED_100baseT_Full;

This is ok for the normal ethtool eee_get call, which always zeroes the input data before.

But eee_set_cur also calls eee_get_cur and it did not zero the input field. Later on it then compares agsinst the field, which can contain partial stack garbage.

Zero the input field in eee_set_cur() too.

---
 drivers/net/ethernet/intel/igb/igb_ethtool.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/net/ethernet/intel/igb/igb_ethtool.c +2 -1
index 48cbc83..41e37ff 100644
@@ -2652,6 +2652,8 @@ static int igb_set_eee(struct net_device *netdev,
 	    (hw->phy.media_type != e1000_media_type_copper))
 		return -EOPNOTSUPP;
 
+	memset(&eee_curr, 0, sizeof(struct ethtool_eee));
+
 	ret_val = igb_get_eee(netdev, &eee_curr);
 	if (ret_val)
 		return ret_val;
--
Quoted 2 lines

1.8.3.1

ACK

Thanks,

Carolyn

Acked-by on v1 7/11
#17 lore raw

Re: [PATCH 07/11] igb: Avoid uninitialized advertised variable in eee_set_cur

In reply to Andi Kleen
Quoted 16 lines · Andi Kleen

From: Andi Kleen <[email protected]>

eee_get_cur assumes that the output data is already zeroed. It can read-modify-write the advertised field:

              if (ipcnfg & E1000_IPCNFG_EEE_100M_AN)

2594 edata->advertised |= ADVERTISED_100baseT_Full;

This is ok for the normal ethtool eee_get call, which always zeroes the input data before.

But eee_set_cur also calls eee_get_cur and it did not zero the input field. Later on it then compares agsinst the field, which can contain partial
stack garbage.

Quoted 8 lines

Zero the input field in eee_set_cur() too.

---
 drivers/net/ethernet/intel/igb/igb_ethtool.c | 2 ++
 1 file changed, 2 insertions(+)
maintainer Applied
#18 lore raw

Re: [PATCH 07/11] igb: Avoid uninitialized advertised variable in eee_set_cur

In reply to Andi Kleen

From: Andi Kleen <[email protected]>
Date: Mon, 30 Sep 2013 13:29:08 -0700

Quoted 15 lines

From: Andi Kleen <[email protected]>

eee_get_cur assumes that the output data is already zeroed. It can read-modify-write the advertised field:

              if (ipcnfg & E1000_IPCNFG_EEE_100M_AN)

2594 edata->advertised |= ADVERTISED_100baseT_Full;

This is ok for the normal ethtool eee_get call, which always zeroes the input data before.

But eee_set_cur also calls eee_get_cur and it did not zero the input field. Later on it then compares agsinst the field, which can contain partial stack garbage.

Quoted 5 lines

Zero the input field in eee_set_cur() too.

Applied.

author
#19 lore raw

PATCH 8/11 ext4: Fix end of group handling in ext4_mb_init_cache

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

The first loop in ext4_mb_init_cache can bail out when the end of all groups is reached. Unfortunately the later loops did not have that check and could access uninitialized buffer pointers in bh[]. Add the end of group check everywhere.

---
 Makefile          | 6 +++---
 fs/ext4/mballoc.c | 6 +++++-
 2 files changed, 8 insertions(+), 4 deletions(-)
Makefile +3 -3
index 8d0668f..be3ef83 100644
@@ -663,9 +663,9 @@ KBUILD_CFLAGS   += $(call cc-option,-fconserve-stack)
 KBUILD_ARFLAGS := $(call ar-option,D)
 
 # check for 'asm goto'
-ifeq ($(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-goto.sh $(CC)), y)
-	KBUILD_CFLAGS += -DCC_HAVE_ASM_GOTO
-endif
+#ifeq ($(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-goto.sh $(CC)), y)
+#	KBUILD_CFLAGS += -DCC_HAVE_ASM_GOTO
+#endif
 
 # Add user supplied CPPFLAGS, AFLAGS and CFLAGS as the last assignments
 KBUILD_CPPFLAGS += $(KCPPFLAGS)
fs/ext4/mballoc.c +5 -1
index a41e3ba..619d8ed 100644
@@ -878,6 +878,8 @@ static int ext4_mb_init_cache(struct page *page, char *incore)
 
 	/* wait for I/O completion */
 	for (i = 0, group = first_group; i < groups_per_page; i++, group++) {
+		if (group >= ngroups)
+			break;
 		if (bh[i] && ext4_wait_block_bitmap(sb, group, bh[i])) {
 			err = -EIO;
 			goto out;
@@ -953,7 +955,9 @@ static int ext4_mb_init_cache(struct page *page, char *incore)
 
 out:
 	if (bh) {
-		for (i = 0; i < groups_per_page; i++)
+		for (i = 0, group = first_group;
+		     i < groups_per_page && group < ngroups;
+		     i++, group++)
 			brelse(bh[i]);
 		if (bh != &bhs)
 			kfree(bh);
Signature · 1 line
1.8.3.1
maintainer
#20 lore raw

Re: [PATCH 08/11] ext4: Fix end of group handling in ext4_mb_init_cache

Quoted 22 lines · Andi Kleen

From: Andi Kleen <[email protected]>

The first loop in ext4_mb_init_cache can bail out when the end of all groups is reached. Unfortunately the later loops did not have that check and could access uninitialized buffer pointers in bh[]. Add the end of group check everywhere.

---
 Makefile          | 6 +++---
 fs/ext4/mballoc.c | 6 +++++-
 2 files changed, 8 insertions(+), 4 deletions(-)
Makefile +0 -0
index 8d0668f..be3ef83 100644
@@ -663,9 +663,9 @@ KBUILD_CFLAGS   += $(call cc-option,-fconserve-stack)
 KBUILD_ARFLAGS := $(call ar-option,D)
 

Makefile KBUILD_CFLAGS += $(call cc-option,-fconserve-stack) · line 671

Quoted 10 lines
Makefile +3 -3
 # check for 'asm goto'
-ifeq ($(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-goto.sh $(CC)), y)
-	KBUILD_CFLAGS += -DCC_HAVE_ASM_GOTO
-endif
+#ifeq ($(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-goto.sh $(CC)), y)
+#	KBUILD_CFLAGS += -DCC_HAVE_ASM_GOTO
+#endif
 
 # Add user supplied CPPFLAGS, AFLAGS and CFLAGS as the last assignments
 KBUILD_CPPFLAGS += $(KCPPFLAGS)

What's this change all about, and why is it included in this patch?

- Ted

author
#21 lore raw

Re: [PATCH 08/11] ext4: Fix end of group handling in ext4_mb_init_cache

Quoted 2 lines

What's this change all about, and why is it included in this patch?

Sorry that was me fat-fingering git add. Ignore that hunk.

I needed it for the static analyzer, which does not understand asm goto.

-Andi

author
#22 lore raw

PATCH 9/11 epoll: Remove unnecessary error path

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

A static checker was pointing out that nothing can possible set nwait < 0 in this path. The comment and the check appears to be outdated. Remove it.

---
 fs/eventpoll.c | 10 ----------
 1 file changed, 10 deletions(-)
fs/eventpoll.c +0 -10
index 473e09d..f72bf55 100644
@@ -1276,15 +1276,6 @@ static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
 	 */
 	revents = ep_item_poll(epi, &epq.pt);
 
-	/*
-	 * We have to check if something went wrong during the poll wait queue
-	 * install process. Namely an allocation for a wait queue failed due
-	 * high memory pressure.
-	 */
-	error = -ENOMEM;
-	if (epi->nwait < 0)
-		goto error_unregister;
-
 	/* Add the current item to the list of active epoll hook for this file */
 	spin_lock(&tfile->f_lock);
 	list_add_tail(&epi->fllink, &tfile->f_ep_links);
@@ -1334,7 +1325,6 @@ error_remove_epi:
 
 	rb_erase(&epi->rbn, &ep->rbr);
 
-error_unregister:
 	ep_unregister_pollwait(ep, epi);
 
 	/*
Signature · 1 line
1.8.3.1
#23 lore raw

Re: [PATCH 09/11] epoll: Remove unnecessary error path

Quoted 5 lines · Andi Kleen

From: Andi Kleen <[email protected]>

A static checker was pointing out that nothing can possible set nwait < 0 in this path. The comment and the check appears to be outdated. Remove it.

I don't think so...

fs/eventpoll.c static int ep_insert(struct eventpoll *ep, struct epoll_event *event, · line 1277

Quoted 13 lines
---
 fs/eventpoll.c | 10 ----------
 1 file changed, 10 deletions(-)
fs/eventpoll.c +0 -0
index 473e09d..f72bf55 100644
@@ -1276,15 +1276,6 @@ static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
 	 */
 	revents = ep_item_poll(epi, &epq.pt);

ep_item_poll calls f_op->poll, which calls poll_wait(). poll_wait() will call ep_ptable_queue_proc.

Quoted 20 lines
fs/eventpoll.c +0 -10
-	/*
-	 * We have to check if something went wrong during the poll wait queue
-	 * install process. Namely an allocation for a wait queue failed due
-	 * high memory pressure.
-	 */
-	error = -ENOMEM;
-	if (epi->nwait < 0)
-		goto error_unregister;
-
 	/* Add the current item to the list of active epoll hook for this file */
 	spin_lock(&tfile->f_lock);
 	list_add_tail(&epi->fllink, &tfile->f_ep_links);
@@ -1334,7 +1325,6 @@ error_remove_epi:
 
 	rb_erase(&epi->rbn, &ep->rbr);
 
-error_unregister:
 	ep_unregister_pollwait(ep, epi);
 
 	/*
author
#24 lore raw

Re: [PATCH 09/11] epoll: Remove unnecessary error path

Quoted 6 lines

fs/eventpoll.c static int ep_insert(struct eventpoll *ep, struct epoll_event *event, · line 1277

Quoted 3 lines
fs/eventpoll.c +0 -0
@@ -1276,15 +1276,6 @@ static int ep_insert(struct eventpoll *ep, struct epoll_event *event,
 	 */
 	revents = ep_item_poll(epi, &epq.pt);

ep_item_poll calls f_op->poll, which calls poll_wait(). poll_wait() will call ep_ptable_queue_proc.

Thanks. Good point.
-Andi

author
#25 lore raw

PATCH 10/11 tcp: Always set options to 0 before calling tcp_established_options

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

tcp_established_options assumes opts->options is 0 before calling, as it read modify writes it.

For the tcp_current_mss() case the opts structure is not zeroed, so this can be done with uninitialized values.

This is ok, because ->options is not read in this path. But it's still better to avoid the operation on the uninitialized field. This shuts up a static code analyzer, and presumably may help the optimizer.

---
 net/ipv4/tcp_output.c | 2 ++
 1 file changed, 2 insertions(+)
net/ipv4/tcp_output.c +2 -0
index 7c83cb8..f3ed78d 100644
@@ -637,6 +637,8 @@ static unsigned int tcp_established_options(struct sock *sk, struct sk_buff *skb
 	unsigned int size = 0;
 	unsigned int eff_sacks;
 
+	opts->options = 0;
+
 #ifdef CONFIG_TCP_MD5SIG
 	*md5 = tp->af_specific->md5_lookup(sk, sk);
 	if (unlikely(*md5)) {
Signature · 1 line
1.8.3.1
maintainer Applied
#26 lore raw

Re: [PATCH 10/11] tcp: Always set options to 0 before calling tcp_established_options

From: Andi Kleen <[email protected]>
Date: Mon, 30 Sep 2013 13:29:11 -0700

Quoted 8 lines

From: Andi Kleen <[email protected]>

tcp_established_options assumes opts->options is 0 before calling, as it read modify writes it.

For the tcp_current_mss() case the opts structure is not zeroed, so this can be done with uninitialized values.

Quoted 7 lines

This is ok, because ->options is not read in this path. But it's still better to avoid the operation on the uninitialized field. This shuts up a static code analyzer, and presumably may help the optimizer.

Applied.

author
#27 lore raw

PATCH 11/11 perf: Avoid uninitialized sample type reference in __perf_event__output_id_sample

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

__perf_event__output_id_sample looks at data->type to decide what to output.

A lot of of the custom output functions, for example perf_log_throttle start with perf_event_header__init_id, which only initializes the header when event->attr.sample_id_all is true.

But when this is false the output function is still called, and will look at an uninitialized header.

I changed all the callers to perf_event_header__init_id to __perf_event_header__init_id which unconditionally initializes the header.

FWIW I'm not fully sure this is the correct fix and what the exact semantics of sample_id_all are supposed to be. Should it disable all throttling etc. messages? Please review carefully.

---
 kernel/events/core.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
kernel/events/core.c +5 -5
index cb4238e..0b15209 100644
@@ -4732,7 +4732,7 @@ perf_event_read_event(struct perf_event *event,
 	};
 	int ret;
 
-	perf_event_header__init_id(&read_event.header, &sample, event);
+	__perf_event_header__init_id(&read_event.header, &sample, event);
 	ret = perf_output_begin(&handle, event, read_event.header.size);
 	if (ret)
 		return;
@@ -4837,7 +4837,7 @@ static void perf_event_task_output(struct perf_event *event,
 	if (!perf_event_task_match(event))
 		return;
 
-	perf_event_header__init_id(&task_event->event_id.header, &sample, event);
+	__perf_event_header__init_id(&task_event->event_id.header, &sample, event);
 
 	ret = perf_output_begin(&handle, event,
 				task_event->event_id.header.size);
@@ -4931,7 +4931,7 @@ static void perf_event_comm_output(struct perf_event *event,
 	if (!perf_event_comm_match(event))
 		return;
 
-	perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
+	__perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
 	ret = perf_output_begin(&handle, event,
 				comm_event->event_id.header.size);
 
@@ -5063,7 +5063,7 @@ static void perf_event_mmap_output(struct perf_event *event,
 		mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
 	}
 
-	perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
+	__perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
 	ret = perf_output_begin(&handle, event,
 				mmap_event->event_id.header.size);
 	if (ret)
@@ -5237,7 +5237,7 @@ static void perf_log_throttle(struct perf_event *event, int enable)
 	if (enable)
 		throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
 
-	perf_event_header__init_id(&throttle_event.header, &sample, event);
+	__perf_event_header__init_id(&throttle_event.header, &sample, event);
 
 	ret = perf_output_begin(&handle, event,
 				throttle_event.header.size);
Signature · 1 line
1.8.3.1
maintainer
#28 lore raw

Re: [PATCH 11/11] perf: Avoid uninitialized sample type reference in __perf_event__output_id_sample

Quoted 12 lines · Andi Kleen

From: Andi Kleen <[email protected]>

__perf_event__output_id_sample looks at data->type to decide what to output.

A lot of of the custom output functions, for example perf_log_throttle start with perf_event_header__init_id, which only initializes the header when event->attr.sample_id_all is true.

But when this is false the output function is still called, and will look at an uninitialized header.

Quoted 8 lines

I changed all the callers to perf_event_header__init_id to __perf_event_header__init_id which unconditionally initializes the header.

FWIW I'm not fully sure this is the correct fix and what the exact semantics of sample_id_all are supposed to be. Should it disable all throttling etc. messages? Please review carefully.

Why are you doing this; also what's up with 11/11?

#29 lore raw

Re: [PATCH 11/11] perf: Avoid uninitialized sample type reference in __perf_event__output_id_sample

Quoted 1 line

Why are you doing this; also what's up with 11/11?

I was playing with a static checker, and posted some of the fixes from looking at the warnings. There were 11 patches in the series

Only one is perf related.

-Andi

Signature · 1 line
[email protected] -- Speaking for myself only
maintainer
#30 lore raw

Re: [PATCH 11/11] perf: Avoid uninitialized sample type reference in __perf_event__output_id_sample

Quoted 7 lines · Andi Kleen
Quoted 1 line

Why are you doing this; also what's up with 11/11?

I was playing with a static checker, and posted some of the fixes from looking at the warnings. There were 11 patches in the series

Only one is perf related.

It would help to know what the reported 'error' was. It wasn't at all clear to me what we were trying to do and why there would be a problem.

#31 lore raw

Re: [PATCH 11/11] perf: Avoid uninitialized sample type reference in __perf_event__output_id_sample

Quoted 10 lines · Peter Zijlstra
Quoted 7 lines · Andi Kleen
Quoted 1 line

Why are you doing this; also what's up with 11/11?

I was playing with a static checker, and posted some of the fixes from looking at the warnings. There were 11 patches in the series

Only one is perf related.

It would help to know what the reported 'error' was. [...]

Forcing multiple, unnecessary rounds of emails instead of clearly volunteering all technical information that is related to the matter is something Andi is still doing routinely.

To deal with this repeat anomaly of Andi's communication patterns I'll just reply with a form letter in the future:

" Your mail or patch is intentionally or accidentally missing key bits of
technical information. The kernel list is an open technical forum where volunteering as much useful information as possible about a specific technical matter _before_ a maintainer asks for it is strongly favored.

The kernel is not a need-to-know security agency where people are routinely faking superior expertise by holding back and hoarding information, forcing others to request that information piecemail wise. "

Thanks,

Ingo

#32 lore raw

Re: [PATCH 11/11] perf: Avoid uninitialized sample type reference in __perf_event__output_id_sample

Quoted 3 lines

Forcing multiple, unnecessary rounds of emails instead of clearly volunteering all technical information that is related to the matter is something Andi is still doing routinely.

Sorry all the information was in the full email thread (including the intro). And the patch description clearly described the problem in your code.

As the patches spanned many subsystems you were only copied on the patches that affect your subsystem, and not on the intro (as git send-email doesn't seem to support that)

Here are possible alternatives. Please let me know which ones you prefer and I'll try to adapt to your specific preferences in the future:

[ ] Always copy you guys on all patches of subsystem spanning patch kits that are mostly of no relevance to you.

[ ] Repeat the complete intro in every patch.

[ ] You think uninitialized are not a problem and you don't want to see any patches related to that.

[ ] You are not interested in fixing static checker problems in your subsystem and don't want to see any patches related to that.

Thanks.

-Andi

#33 lore raw

Re: [PATCH 11/11] perf: Avoid uninitialized sample type reference in __perf_event__output_id_sample

Quoted 19 lines · Andi Kleen
Quoted 3 lines

Forcing multiple, unnecessary rounds of emails instead of clearly volunteering all technical information that is related to the matter is something Andi is still doing routinely.

Sorry all the information was in the full email thread (including the intro). And the patch description clearly described the problem in your code.

As the patches spanned many subsystems you were only copied on the patches that affect your subsystem, and not on the intro (as git send-email doesn't seem to support that)

Here are possible alternatives. Please let me know which ones you prefer and I'll try to adapt to your specific preferences in the future:

[ ] Always copy you guys on all patches of subsystem spanning patch kits that are mostly of no relevance to you.

Quoted 7 lines

[ ] Repeat the complete intro in every patch.

[ ] You think uninitialized are not a problem and you don't want to see any patches related to that.

[ ] You are not interested in fixing static checker problems in your subsystem and don't want to see any patches related to that.

[x] Fix your changelog. Any commit log that is not self-describing to a maintainer like PeterZ is faulty by definition - regardless of any intro somewhere else. Changelogs ultimately detach from intros, they will be read by others in the kernel repo, reviewing changes, looking for bugs, following development, etc., and if the relevant context is lost then that changelog is _BROKEN_. This is kernel development 101.

[x] You should stop making idiotic excuses for broken changelogs. The fact that we had to complain about the quality of your patches and changelogs for years should give you a big, huge hint that the problem is still on your side. Writing snarky responses to maintainer questions does not improve your changelogs, it only wastes PeterZ's, my and other people's time.

Thanks,

Ingo