Skip to content

Commit 17911ff

Browse files
committed
tracing: Add locked_down checks to the open calls of files created for tracefs
Added various checks on open tracefs calls to see if tracefs is in lockdown mode, and if so, to return -EPERM. Note, the event format files (which are basically standard on all machines) as well as the enabled_functions file (which shows what is currently being traced) are not lockde down. Perhaps they should be, but it seems counter intuitive to lockdown information to help you know if the system has been modified. Link: http://lkml.kernel.org/r/CAHk-=wj7fGPKUspr579Cii-w_y60PtRaiDgKuxVtBAMK0VNNkA@mail.gmail.com Suggested-by: Linus Torvalds <[email protected]> Signed-off-by: Steven Rostedt (VMware) <[email protected]>
1 parent 8530dec commit 17911ff

File tree

10 files changed

+98
-4
lines changed

10 files changed

+98
-4
lines changed

kernel/trace/ftrace.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/clocksource.h>
1919
#include <linux/sched/task.h>
2020
#include <linux/kallsyms.h>
21+
#include <linux/security.h>
2122
#include <linux/seq_file.h>
2223
#include <linux/tracefs.h>
2324
#include <linux/hardirq.h>
@@ -3486,6 +3487,11 @@ static int
34863487
ftrace_avail_open(struct inode *inode, struct file *file)
34873488
{
34883489
struct ftrace_iterator *iter;
3490+
int ret;
3491+
3492+
ret = security_locked_down(LOCKDOWN_TRACEFS);
3493+
if (ret)
3494+
return ret;
34893495

34903496
if (unlikely(ftrace_disabled))
34913497
return -ENODEV;
@@ -3505,6 +3511,15 @@ ftrace_enabled_open(struct inode *inode, struct file *file)
35053511
{
35063512
struct ftrace_iterator *iter;
35073513

3514+
/*
3515+
* This shows us what functions are currently being
3516+
* traced and by what. Not sure if we want lockdown
3517+
* to hide such critical information for an admin.
3518+
* Although, perhaps it can show information we don't
3519+
* want people to see, but if something is tracing
3520+
* something, we probably want to know about it.
3521+
*/
3522+
35083523
iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
35093524
if (!iter)
35103525
return -ENOMEM;
@@ -3625,6 +3640,7 @@ ftrace_filter_open(struct inode *inode, struct file *file)
36253640
{
36263641
struct ftrace_ops *ops = inode->i_private;
36273642

3643+
/* Checks for tracefs lockdown */
36283644
return ftrace_regex_open(ops,
36293645
FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
36303646
inode, file);
@@ -3635,6 +3651,7 @@ ftrace_notrace_open(struct inode *inode, struct file *file)
36353651
{
36363652
struct ftrace_ops *ops = inode->i_private;
36373653

3654+
/* Checks for tracefs lockdown */
36383655
return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
36393656
inode, file);
36403657
}
@@ -5203,9 +5220,13 @@ static int
52035220
__ftrace_graph_open(struct inode *inode, struct file *file,
52045221
struct ftrace_graph_data *fgd)
52055222
{
5206-
int ret = 0;
5223+
int ret;
52075224
struct ftrace_hash *new_hash = NULL;
52085225

5226+
ret = security_locked_down(LOCKDOWN_TRACEFS);
5227+
if (ret)
5228+
return ret;
5229+
52095230
if (file->f_mode & FMODE_WRITE) {
52105231
const int size_bits = FTRACE_HASH_DEFAULT_BITS;
52115232

kernel/trace/trace.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <linux/stacktrace.h>
1818
#include <linux/writeback.h>
1919
#include <linux/kallsyms.h>
20+
#include <linux/security.h>
2021
#include <linux/seq_file.h>
2122
#include <linux/notifier.h>
2223
#include <linux/irqflags.h>
@@ -306,6 +307,12 @@ void trace_array_put(struct trace_array *this_tr)
306307

307308
int tracing_check_open_get_tr(struct trace_array *tr)
308309
{
310+
int ret;
311+
312+
ret = security_locked_down(LOCKDOWN_TRACEFS);
313+
if (ret)
314+
return ret;
315+
309316
if (tracing_disabled)
310317
return -ENODEV;
311318

@@ -6813,6 +6820,7 @@ static int snapshot_raw_open(struct inode *inode, struct file *filp)
68136820
struct ftrace_buffer_info *info;
68146821
int ret;
68156822

6823+
/* The following checks for tracefs lockdown */
68166824
ret = tracing_buffers_open(inode, filp);
68176825
if (ret < 0)
68186826
return ret;

kernel/trace/trace_events.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#define pr_fmt(fmt) fmt
1313

1414
#include <linux/workqueue.h>
15+
#include <linux/security.h>
1516
#include <linux/spinlock.h>
1617
#include <linux/kthread.h>
1718
#include <linux/tracefs.h>
@@ -1294,6 +1295,8 @@ static int trace_format_open(struct inode *inode, struct file *file)
12941295
struct seq_file *m;
12951296
int ret;
12961297

1298+
/* Do we want to hide event format files on tracefs lockdown? */
1299+
12971300
ret = seq_open(file, &trace_format_seq_ops);
12981301
if (ret < 0)
12991302
return ret;
@@ -1760,6 +1763,10 @@ ftrace_event_open(struct inode *inode, struct file *file,
17601763
struct seq_file *m;
17611764
int ret;
17621765

1766+
ret = security_locked_down(LOCKDOWN_TRACEFS);
1767+
if (ret)
1768+
return ret;
1769+
17631770
ret = seq_open(file, seq_ops);
17641771
if (ret < 0)
17651772
return ret;
@@ -1784,6 +1791,7 @@ ftrace_event_avail_open(struct inode *inode, struct file *file)
17841791
{
17851792
const struct seq_operations *seq_ops = &show_event_seq_ops;
17861793

1794+
/* Checks for tracefs lockdown */
17871795
return ftrace_event_open(inode, file, seq_ops);
17881796
}
17891797

kernel/trace/trace_events_hist.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <linux/module.h>
99
#include <linux/kallsyms.h>
10+
#include <linux/security.h>
1011
#include <linux/mutex.h>
1112
#include <linux/slab.h>
1213
#include <linux/stacktrace.h>
@@ -1448,6 +1449,10 @@ static int synth_events_open(struct inode *inode, struct file *file)
14481449
{
14491450
int ret;
14501451

1452+
ret = security_locked_down(LOCKDOWN_TRACEFS);
1453+
if (ret)
1454+
return ret;
1455+
14511456
if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
14521457
ret = dyn_events_release_all(&synth_event_ops);
14531458
if (ret < 0)
@@ -5515,6 +5520,12 @@ static int hist_show(struct seq_file *m, void *v)
55155520

55165521
static int event_hist_open(struct inode *inode, struct file *file)
55175522
{
5523+
int ret;
5524+
5525+
ret = security_locked_down(LOCKDOWN_TRACEFS);
5526+
if (ret)
5527+
return ret;
5528+
55185529
return single_open(file, hist_show, file);
55195530
}
55205531

kernel/trace/trace_events_trigger.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Copyright (C) 2013 Tom Zanussi <[email protected]>
66
*/
77

8+
#include <linux/security.h>
89
#include <linux/module.h>
910
#include <linux/ctype.h>
1011
#include <linux/mutex.h>
@@ -173,7 +174,11 @@ static const struct seq_operations event_triggers_seq_ops = {
173174

174175
static int event_trigger_regex_open(struct inode *inode, struct file *file)
175176
{
176-
int ret = 0;
177+
int ret;
178+
179+
ret = security_locked_down(LOCKDOWN_TRACEFS);
180+
if (ret)
181+
return ret;
177182

178183
mutex_lock(&event_mutex);
179184

@@ -292,6 +297,7 @@ event_trigger_write(struct file *filp, const char __user *ubuf,
292297
static int
293298
event_trigger_open(struct inode *inode, struct file *filp)
294299
{
300+
/* Checks for tracefs lockdown */
295301
return event_trigger_regex_open(inode, filp);
296302
}
297303

kernel/trace/trace_kprobe.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
*/
88
#define pr_fmt(fmt) "trace_kprobe: " fmt
99

10+
#include <linux/security.h>
1011
#include <linux/module.h>
1112
#include <linux/uaccess.h>
1213
#include <linux/rculist.h>
1314
#include <linux/error-injection.h>
14-
#include <linux/security.h>
1515

1616
#include <asm/setup.h> /* for COMMAND_LINE_SIZE */
1717

@@ -936,6 +936,10 @@ static int probes_open(struct inode *inode, struct file *file)
936936
{
937937
int ret;
938938

939+
ret = security_locked_down(LOCKDOWN_TRACEFS);
940+
if (ret)
941+
return ret;
942+
939943
if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
940944
ret = dyn_events_release_all(&trace_kprobe_ops);
941945
if (ret < 0)
@@ -988,6 +992,12 @@ static const struct seq_operations profile_seq_op = {
988992

989993
static int profile_open(struct inode *inode, struct file *file)
990994
{
995+
int ret;
996+
997+
ret = security_locked_down(LOCKDOWN_TRACEFS);
998+
if (ret)
999+
return ret;
1000+
9911001
return seq_open(file, &profile_seq_op);
9921002
}
9931003

kernel/trace/trace_printk.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*
77
*/
88
#include <linux/seq_file.h>
9+
#include <linux/security.h>
910
#include <linux/uaccess.h>
1011
#include <linux/kernel.h>
1112
#include <linux/ftrace.h>
@@ -348,6 +349,12 @@ static const struct seq_operations show_format_seq_ops = {
348349
static int
349350
ftrace_formats_open(struct inode *inode, struct file *file)
350351
{
352+
int ret;
353+
354+
ret = security_locked_down(LOCKDOWN_TRACEFS);
355+
if (ret)
356+
return ret;
357+
351358
return seq_open(file, &show_format_seq_ops);
352359
}
353360

kernel/trace/trace_stack.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
#include <linux/sched/task_stack.h>
77
#include <linux/stacktrace.h>
8+
#include <linux/security.h>
89
#include <linux/kallsyms.h>
910
#include <linux/seq_file.h>
1011
#include <linux/spinlock.h>
@@ -470,6 +471,12 @@ static const struct seq_operations stack_trace_seq_ops = {
470471

471472
static int stack_trace_open(struct inode *inode, struct file *file)
472473
{
474+
int ret;
475+
476+
ret = security_locked_down(LOCKDOWN_TRACEFS);
477+
if (ret)
478+
return ret;
479+
473480
return seq_open(file, &stack_trace_seq_ops);
474481
}
475482

@@ -487,6 +494,7 @@ stack_trace_filter_open(struct inode *inode, struct file *file)
487494
{
488495
struct ftrace_ops *ops = inode->i_private;
489496

497+
/* Checks for tracefs lockdown */
490498
return ftrace_regex_open(ops, FTRACE_ITER_FILTER,
491499
inode, file);
492500
}

kernel/trace/trace_stat.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*/
1111

12-
12+
#include <linux/security.h>
1313
#include <linux/list.h>
1414
#include <linux/slab.h>
1515
#include <linux/rbtree.h>
@@ -238,6 +238,10 @@ static int tracing_stat_open(struct inode *inode, struct file *file)
238238
struct seq_file *m;
239239
struct stat_session *session = inode->i_private;
240240

241+
ret = security_locked_down(LOCKDOWN_TRACEFS);
242+
if (ret)
243+
return ret;
244+
241245
ret = stat_seq_init(session);
242246
if (ret)
243247
return ret;

kernel/trace/trace_uprobe.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
#define pr_fmt(fmt) "trace_uprobe: " fmt
99

10+
#include <linux/security.h>
1011
#include <linux/ctype.h>
1112
#include <linux/module.h>
1213
#include <linux/uaccess.h>
@@ -769,6 +770,10 @@ static int probes_open(struct inode *inode, struct file *file)
769770
{
770771
int ret;
771772

773+
ret = security_locked_down(LOCKDOWN_TRACEFS);
774+
if (ret)
775+
return ret;
776+
772777
if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
773778
ret = dyn_events_release_all(&trace_uprobe_ops);
774779
if (ret)
@@ -818,6 +823,12 @@ static const struct seq_operations profile_seq_op = {
818823

819824
static int profile_open(struct inode *inode, struct file *file)
820825
{
826+
int ret;
827+
828+
ret = security_locked_down(LOCKDOWN_TRACEFS);
829+
if (ret)
830+
return ret;
831+
821832
return seq_open(file, &profile_seq_op);
822833
}
823834

0 commit comments

Comments
 (0)