Skip to content

Commit 25ddbb1

Browse files
Andi Kleentorvalds
authored andcommitted
Make the taint flags reliable
It's somewhat unlikely that it happens, but right now a race window between interrupts or machine checks or oopses could corrupt the tainted bitmap because it is modified in a non atomic fashion. Convert the taint variable to an unsigned long and use only atomic bit operations on it. Unfortunately this means the intvec sysctl functions cannot be used on it anymore. It turned out the taint sysctl handler could actually be simplified a bit (since it only increases capabilities) so this patch actually removes code. [akpm@linux-foundation.org: remove unneeded include] Signed-off-by: Andi Kleen <ak@linux.intel.com> Cc: Ingo Molnar <mingo@elte.hu> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 889d51a commit 25ddbb1

6 files changed

Lines changed: 101 additions & 82 deletions

File tree

‎arch/x86/kernel/smpboot.c‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,8 @@ static void __cpuinit smp_callin(void)
282282
cpu_set(cpuid, cpu_callin_map);
283283
}
284284

285+
static int __cpuinitdata unsafe_smp;
286+
285287
/*
286288
* Activate a secondary processor.
287289
*/
@@ -397,7 +399,7 @@ static void __cpuinit smp_apply_quirks(struct cpuinfo_x86 *c)
397399
goto valid_k7;
398400

399401
/* If we get here, not a certified SMP capable AMD system. */
400-
add_taint(TAINT_UNSAFE_SMP);
402+
unsafe_smp = 1;
401403
}
402404

403405
valid_k7:
@@ -414,12 +416,10 @@ static void __cpuinit smp_checks(void)
414416
* Don't taint if we are running SMP kernel on a single non-MP
415417
* approved Athlon
416418
*/
417-
if (tainted & TAINT_UNSAFE_SMP) {
418-
if (num_online_cpus())
419-
printk(KERN_INFO "WARNING: This combination of AMD"
420-
"processors is not suitable for SMP.\n");
421-
else
422-
tainted &= ~TAINT_UNSAFE_SMP;
419+
if (unsafe_smp && num_online_cpus() > 1) {
420+
printk(KERN_INFO "WARNING: This combination of AMD"
421+
"processors is not suitable for SMP.\n");
422+
add_taint(TAINT_UNSAFE_SMP);
423423
}
424424
}
425425

‎include/linux/kernel.h‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,10 @@ extern int oops_in_progress; /* If set, an oops, panic(), BUG() or die() is in
235235
extern int panic_timeout;
236236
extern int panic_on_oops;
237237
extern int panic_on_unrecovered_nmi;
238-
extern int tainted;
239238
extern const char *print_tainted(void);
240-
extern void add_taint(unsigned);
239+
extern void add_taint(unsigned flag);
240+
extern int test_taint(unsigned flag);
241+
extern unsigned long get_taint(void);
241242
extern int root_mountflags;
242243

243244
/* Values used for system_state */
@@ -250,16 +251,16 @@ extern enum system_states {
250251
SYSTEM_SUSPEND_DISK,
251252
} system_state;
252253

253-
#define TAINT_PROPRIETARY_MODULE (1<<0)
254-
#define TAINT_FORCED_MODULE (1<<1)
255-
#define TAINT_UNSAFE_SMP (1<<2)
256-
#define TAINT_FORCED_RMMOD (1<<3)
257-
#define TAINT_MACHINE_CHECK (1<<4)
258-
#define TAINT_BAD_PAGE (1<<5)
259-
#define TAINT_USER (1<<6)
260-
#define TAINT_DIE (1<<7)
261-
#define TAINT_OVERRIDDEN_ACPI_TABLE (1<<8)
262-
#define TAINT_WARN (1<<9)
254+
#define TAINT_PROPRIETARY_MODULE 0
255+
#define TAINT_FORCED_MODULE 1
256+
#define TAINT_UNSAFE_SMP 2
257+
#define TAINT_FORCED_RMMOD 3
258+
#define TAINT_MACHINE_CHECK 4
259+
#define TAINT_BAD_PAGE 5
260+
#define TAINT_USER 6
261+
#define TAINT_DIE 7
262+
#define TAINT_OVERRIDDEN_ACPI_TABLE 8
263+
#define TAINT_WARN 9
263264

264265
extern void dump_stack(void) __cold;
265266

‎kernel/module.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ static inline int strong_try_module_get(struct module *mod)
100100
static inline void add_taint_module(struct module *mod, unsigned flag)
101101
{
102102
add_taint(flag);
103-
mod->taints |= flag;
103+
mod->taints |= (1U << flag);
104104
}
105105

106106
/*
@@ -923,7 +923,7 @@ static const char vermagic[] = VERMAGIC_STRING;
923923
static int try_to_force_load(struct module *mod, const char *symname)
924924
{
925925
#ifdef CONFIG_MODULE_FORCE_LOAD
926-
if (!(tainted & TAINT_FORCED_MODULE))
926+
if (!test_taint(TAINT_FORCED_MODULE))
927927
printk("%s: no version for \"%s\" found: kernel tainted.\n",
928928
mod->name, symname);
929929
add_taint_module(mod, TAINT_FORCED_MODULE);
@@ -1033,7 +1033,7 @@ static unsigned long resolve_symbol(Elf_Shdr *sechdrs,
10331033
const unsigned long *crc;
10341034

10351035
ret = find_symbol(name, &owner, &crc,
1036-
!(mod->taints & TAINT_PROPRIETARY_MODULE), true);
1036+
!(mod->taints & (1 << TAINT_PROPRIETARY_MODULE)), true);
10371037
if (!IS_ERR_VALUE(ret)) {
10381038
/* use_module can fail due to OOM,
10391039
or module initialization or unloading */
@@ -1634,7 +1634,7 @@ static void set_license(struct module *mod, const char *license)
16341634
license = "unspecified";
16351635

16361636
if (!license_is_gpl_compatible(license)) {
1637-
if (!(tainted & TAINT_PROPRIETARY_MODULE))
1637+
if (!test_taint(TAINT_PROPRIETARY_MODULE))
16381638
printk(KERN_WARNING "%s: module license '%s' taints "
16391639
"kernel.\n", mod->name, license);
16401640
add_taint_module(mod, TAINT_PROPRIETARY_MODULE);
@@ -2552,9 +2552,9 @@ static char *module_flags(struct module *mod, char *buf)
25522552
mod->state == MODULE_STATE_GOING ||
25532553
mod->state == MODULE_STATE_COMING) {
25542554
buf[bx++] = '(';
2555-
if (mod->taints & TAINT_PROPRIETARY_MODULE)
2555+
if (mod->taints & (1 << TAINT_PROPRIETARY_MODULE))
25562556
buf[bx++] = 'P';
2557-
if (mod->taints & TAINT_FORCED_MODULE)
2557+
if (mod->taints & (1 << TAINT_FORCED_MODULE))
25582558
buf[bx++] = 'F';
25592559
/*
25602560
* TAINT_FORCED_RMMOD: could be added.

‎kernel/panic.c‎

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include <linux/kallsyms.h>
2424

2525
int panic_on_oops;
26-
int tainted;
26+
static unsigned long tainted_mask;
2727
static int pause_on_oops;
2828
static int pause_on_oops_flag;
2929
static DEFINE_SPINLOCK(pause_on_oops_lock);
@@ -159,31 +159,60 @@ EXPORT_SYMBOL(panic);
159159
* The string is overwritten by the next call to print_taint().
160160
*/
161161

162+
struct tnt {
163+
u8 bit;
164+
char true;
165+
char false;
166+
};
167+
168+
static const struct tnt tnts[] = {
169+
{ TAINT_PROPRIETARY_MODULE, 'P', 'G' },
170+
{ TAINT_FORCED_MODULE, 'F', ' ' },
171+
{ TAINT_UNSAFE_SMP, 'S', ' ' },
172+
{ TAINT_FORCED_RMMOD, 'R', ' ' },
173+
{ TAINT_MACHINE_CHECK, 'M', ' ' },
174+
{ TAINT_BAD_PAGE, 'B', ' ' },
175+
{ TAINT_USER, 'U', ' ' },
176+
{ TAINT_DIE, 'D', ' ' },
177+
{ TAINT_OVERRIDDEN_ACPI_TABLE, 'A', ' ' },
178+
{ TAINT_WARN, 'W', ' ' },
179+
};
180+
162181
const char *print_tainted(void)
163182
{
164-
static char buf[20];
165-
if (tainted) {
166-
snprintf(buf, sizeof(buf), "Tainted: %c%c%c%c%c%c%c%c%c%c",
167-
tainted & TAINT_PROPRIETARY_MODULE ? 'P' : 'G',
168-
tainted & TAINT_FORCED_MODULE ? 'F' : ' ',
169-
tainted & TAINT_UNSAFE_SMP ? 'S' : ' ',
170-
tainted & TAINT_FORCED_RMMOD ? 'R' : ' ',
171-
tainted & TAINT_MACHINE_CHECK ? 'M' : ' ',
172-
tainted & TAINT_BAD_PAGE ? 'B' : ' ',
173-
tainted & TAINT_USER ? 'U' : ' ',
174-
tainted & TAINT_DIE ? 'D' : ' ',
175-
tainted & TAINT_OVERRIDDEN_ACPI_TABLE ? 'A' : ' ',
176-
tainted & TAINT_WARN ? 'W' : ' ');
177-
}
178-
else
183+
static char buf[ARRAY_SIZE(tnts) + sizeof("Tainted: ") + 1];
184+
185+
if (tainted_mask) {
186+
char *s;
187+
int i;
188+
189+
s = buf + sprintf(buf, "Tainted: ");
190+
for (i = 0; i < ARRAY_SIZE(tnts); i++) {
191+
const struct tnt *t = &tnts[i];
192+
*s++ = test_bit(t->bit, &tainted_mask) ?
193+
t->true : t->false;
194+
}
195+
*s = 0;
196+
} else
179197
snprintf(buf, sizeof(buf), "Not tainted");
180198
return(buf);
181199
}
182200

201+
int test_taint(unsigned flag)
202+
{
203+
return test_bit(flag, &tainted_mask);
204+
}
205+
EXPORT_SYMBOL(test_taint);
206+
207+
unsigned long get_taint(void)
208+
{
209+
return tainted_mask;
210+
}
211+
183212
void add_taint(unsigned flag)
184213
{
185214
debug_locks = 0; /* can't trust the integrity of the kernel anymore */
186-
tainted |= flag;
215+
set_bit(flag, &tainted_mask);
187216
}
188217
EXPORT_SYMBOL(add_taint);
189218

‎kernel/softlockup.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ static void check_hung_uninterruptible_tasks(int this_cpu)
226226
* If the system crashed already then all bets are off,
227227
* do not report extra hung tasks:
228228
*/
229-
if ((tainted & TAINT_DIE) || did_panic)
229+
if (test_taint(TAINT_DIE) || did_panic)
230230
return;
231231

232232
read_lock(&tasklist_lock);

‎kernel/sysctl.c‎

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ extern int max_lock_depth;
149149
#ifdef CONFIG_PROC_SYSCTL
150150
static int proc_do_cad_pid(struct ctl_table *table, int write, struct file *filp,
151151
void __user *buffer, size_t *lenp, loff_t *ppos);
152-
static int proc_dointvec_taint(struct ctl_table *table, int write, struct file *filp,
152+
static int proc_taint(struct ctl_table *table, int write, struct file *filp,
153153
void __user *buffer, size_t *lenp, loff_t *ppos);
154154
#endif
155155

@@ -379,10 +379,9 @@ static struct ctl_table kern_table[] = {
379379
#ifdef CONFIG_PROC_SYSCTL
380380
{
381381
.procname = "tainted",
382-
.data = &tainted,
383-
.maxlen = sizeof(int),
382+
.maxlen = sizeof(long),
384383
.mode = 0644,
385-
.proc_handler = &proc_dointvec_taint,
384+
.proc_handler = &proc_taint,
386385
},
387386
#endif
388387
#ifdef CONFIG_LATENCYTOP
@@ -2228,49 +2227,39 @@ int proc_dointvec(struct ctl_table *table, int write, struct file *filp,
22282227
NULL,NULL);
22292228
}
22302229

2231-
#define OP_SET 0
2232-
#define OP_AND 1
2233-
#define OP_OR 2
2234-
2235-
static int do_proc_dointvec_bset_conv(int *negp, unsigned long *lvalp,
2236-
int *valp,
2237-
int write, void *data)
2238-
{
2239-
int op = *(int *)data;
2240-
if (write) {
2241-
int val = *negp ? -*lvalp : *lvalp;
2242-
switch(op) {
2243-
case OP_SET: *valp = val; break;
2244-
case OP_AND: *valp &= val; break;
2245-
case OP_OR: *valp |= val; break;
2246-
}
2247-
} else {
2248-
int val = *valp;
2249-
if (val < 0) {
2250-
*negp = -1;
2251-
*lvalp = (unsigned long)-val;
2252-
} else {
2253-
*negp = 0;
2254-
*lvalp = (unsigned long)val;
2255-
}
2256-
}
2257-
return 0;
2258-
}
2259-
22602230
/*
2261-
* Taint values can only be increased
2231+
* Taint values can only be increased
2232+
* This means we can safely use a temporary.
22622233
*/
2263-
static int proc_dointvec_taint(struct ctl_table *table, int write, struct file *filp,
2234+
static int proc_taint(struct ctl_table *table, int write, struct file *filp,
22642235
void __user *buffer, size_t *lenp, loff_t *ppos)
22652236
{
2266-
int op;
2237+
struct ctl_table t;
2238+
unsigned long tmptaint = get_taint();
2239+
int err;
22672240

22682241
if (write && !capable(CAP_SYS_ADMIN))
22692242
return -EPERM;
22702243

2271-
op = OP_OR;
2272-
return do_proc_dointvec(table,write,filp,buffer,lenp,ppos,
2273-
do_proc_dointvec_bset_conv,&op);
2244+
t = *table;
2245+
t.data = &tmptaint;
2246+
err = proc_doulongvec_minmax(&t, write, filp, buffer, lenp, ppos);
2247+
if (err < 0)
2248+
return err;
2249+
2250+
if (write) {
2251+
/*
2252+
* Poor man's atomic or. Not worth adding a primitive
2253+
* to everyone's atomic.h for this
2254+
*/
2255+
int i;
2256+
for (i = 0; i < BITS_PER_LONG && tmptaint >> i; i++) {
2257+
if ((tmptaint >> i) & 1)
2258+
add_taint(i);
2259+
}
2260+
}
2261+
2262+
return err;
22742263
}
22752264

22762265
struct do_proc_dointvec_minmax_conv_param {

0 commit comments

Comments
 (0)