Skip to contentSkip to conversation

KDB: Kiosk (reduced capabilities) mode

PATCH In review Anton Vorontsov 5 participants 15 messages Jul 26, 2012 – Jul 30, 2012

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

cover KDB: Kiosk (reduced capabilities) mode

Hi all,

Here is a patchset that implements "kiosk" mode for KDB debugger. The mode provides reduced set of features, so that it is no longer possible to leak sensitive data via the debugger, and not possible to change program flow in a predefined manner.

The are two use-cases for the mode, one is evil, but another is quite legitimate.

The evil use case is used by some (ahem) phone manufaturers that want to have a debuging facilities on a production device, but still don't want you to use the debugger to gain root access. I don't like locked phones, and I would not touch this/get my hands dirty by implementing the feature just for this evil (IMHO) use case.

But there is another non-evil use case: limitting access to public devices, i.e. "kiosks", ATMs (is that too much?) or just public computers w/ guest access. I can imagine that an administrator would want to setup a kernel so that upon an oops (or a sysrq event) the kernel would enter KDB, but at the same time, he would not want to leak sensitive data from the PC by means of the debugger.

There are seven patches, the first five of them are just cleanups and preparations. I believe these five patches are good even if not considering the kiosk mode. And the rest of patches actually implement the mode -- it is pretty straightforward.

Note that we might impelement the same mode for KGDB stub, but so far we don't bother.

Thanks!

--

 include/linux/kdb.h            |   16 ++--
 kernel/debug/kdb/kdb_bp.c      |   35 ++++----
 kernel/debug/kdb/kdb_main.c    |  183 +++++++++++++++++++++-------------------
 kernel/debug/kdb/kdb_private.h |    3 +-
 kernel/trace/trace_kdb.c       |    4 +-
 5 files changed, 126 insertions(+), 115 deletions(-)
Signature · 2 lines
Anton Vorontsov
Email: [email protected]
#1 lore raw

PATCH 1/7 kdb: Remove currently unused kdbtab_t->cmd_flags

The struct member is never used in the code, so we can remove it.

We will introduce real flags soon by renaming cmd_repeat to cmd_flags.

---
 kernel/debug/kdb/kdb_main.c    |    1 -
 kernel/debug/kdb/kdb_private.h |    1 -
 2 files changed, 2 deletions(-)
kernel/debug/kdb/kdb_main.c +0 -1
index 67b847d..e2b14ea 100644
@@ -2757,7 +2757,6 @@ int kdb_register_repeat(char *cmd,
 	kp->cmd_func   = func;
 	kp->cmd_usage  = usage;
 	kp->cmd_help   = help;
-	kp->cmd_flags  = 0;
 	kp->cmd_minlen = minlen;
 	kp->cmd_repeat = repeat;
 
kernel/debug/kdb/kdb_private.h +0 -1
index 47c4e56..b7869c7 100644
@@ -175,7 +175,6 @@ typedef struct _kdbtab {
 	kdb_func_t cmd_func;		/* Function to execute command */
 	char    *cmd_usage;		/* Usage String for this command */
 	char    *cmd_help;		/* Help message for this command */
-	short    cmd_flags;		/* Parsing flags */
 	short    cmd_minlen;		/* Minimum legal # command
 					 * chars required */
 	kdb_repeat_t cmd_repeat;	/* Does command auto repeat on enter? */
Signature · 1 line
1.7.10.4
#2 lore raw

PATCH 2/7 kdb: Rename kdb_repeat_t to kdb_cmdflags_t, cmd_repeat to cmd_flags

In reply to Anton Vorontsov

We're about to add more options for command behaviour, so let's expand the meaning of kdb_repeat_t.

So far we just do various renames, there should be no functional changes.

---
 include/linux/kdb.h            |    4 ++--
 kernel/debug/kdb/kdb_main.c    |    6 +++---
 kernel/debug/kdb/kdb_private.h |    2 +-
 3 files changed, 6 insertions(+), 6 deletions(-)
include/linux/kdb.h +2 -2
index 0647258..029219c 100644
@@ -36,7 +36,7 @@ typedef enum {
 	KDB_REPEAT_NONE = 0,	/* Do not repeat this command */
 	KDB_REPEAT_NO_ARGS,	/* Repeat the command without arguments */
 	KDB_REPEAT_WITH_ARGS,	/* Repeat the command including its arguments */
-} kdb_repeat_t;
+} kdb_cmdflags_t;
 
 typedef int (*kdb_func_t)(int, const char **);
 
@@ -148,7 +148,7 @@ static inline const char *kdb_walk_kallsyms(loff_t *pos)
 /* Dynamic kdb shell command registration */
 extern int kdb_register(char *, kdb_func_t, char *, char *, short);
 extern int kdb_register_repeat(char *, kdb_func_t, char *, char *,
-			       short, kdb_repeat_t);
+			       short, kdb_cmdflags_t);
 extern int kdb_unregister(char *);
 #else /* ! CONFIG_KGDB_KDB */
 #define kdb_printf(...)
kernel/debug/kdb/kdb_main.c +3 -3
index e2b14ea..c8e1c7b 100644
@@ -991,7 +991,7 @@ int kdb_parse(const char *cmdstr)
 		if (result && ignore_errors && result > KDB_CMD_GO)
 			result = 0;
 		KDB_STATE_CLEAR(CMD);
-		switch (tp->cmd_repeat) {
+		switch (tp->cmd_flags) {
 		case KDB_REPEAT_NONE:
 			argc = 0;
 			if (argv[0])
@@ -2709,7 +2709,7 @@ int kdb_register_repeat(char *cmd,
 			char *usage,
 			char *help,
 			short minlen,
-			kdb_repeat_t repeat)
+			kdb_cmdflags_t flags)
 {
 	int i;
 	kdbtab_t *kp;
@@ -2758,7 +2758,7 @@ int kdb_register_repeat(char *cmd,
 	kp->cmd_usage  = usage;
 	kp->cmd_help   = help;
 	kp->cmd_minlen = minlen;
-	kp->cmd_repeat = repeat;
+	kp->cmd_flags  = flags;
 
 	return 0;
 }
kernel/debug/kdb/kdb_private.h +1 -1
index b7869c7..9665af6 100644
@@ -177,7 +177,7 @@ typedef struct _kdbtab {
 	char    *cmd_help;		/* Help message for this command */
 	short    cmd_minlen;		/* Minimum legal # command
 					 * chars required */
-	kdb_repeat_t cmd_repeat;	/* Does command auto repeat on enter? */
+	kdb_cmdflags_t cmd_flags;	/* Command behaviour flags */
 } kdbtab_t;
 
 extern int kdb_bt(int, const char **);	/* KDB display back trace */
Signature · 1 line
1.7.10.4
#3 lore raw

PATCH 3/7 kdb: Rename kdb_register_repeat() to kdb_register_flags()

In reply to Anton Vorontsov

We're about to add more options for commands behaviour, so let's give a more generic name to the low-level kdb command registration function.

There are just various renames, no functional changes.

---
 include/linux/kdb.h         |    6 +--
 kernel/debug/kdb/kdb_bp.c   |   16 ++++----
 kernel/debug/kdb/kdb_main.c |   86 +++++++++++++++++++++----------------------
 kernel/trace/trace_kdb.c    |    2 +-
 4 files changed, 55 insertions(+), 55 deletions(-)
include/linux/kdb.h +3 -3
index 029219c..4ab0936 100644
@@ -147,14 +147,14 @@ static inline const char *kdb_walk_kallsyms(loff_t *pos)
 
 /* Dynamic kdb shell command registration */
 extern int kdb_register(char *, kdb_func_t, char *, char *, short);
-extern int kdb_register_repeat(char *, kdb_func_t, char *, char *,
-			       short, kdb_cmdflags_t);
+extern int kdb_register_flags(char *, kdb_func_t, char *, char *,
+			      short, kdb_cmdflags_t);
 extern int kdb_unregister(char *);
 #else /* ! CONFIG_KGDB_KDB */
 #define kdb_printf(...)
 #define kdb_init(x)
 #define kdb_register(...)
-#define kdb_register_repeat(...)
+#define kdb_register_flags(...)
 #define kdb_uregister(x)
 #endif	/* CONFIG_KGDB_KDB */
 enum {
kernel/debug/kdb/kdb_bp.c +8 -8
index 8418c2f..d2cb80d 100644
@@ -545,23 +545,23 @@ void __init kdb_initbptab(void)
 	for (i = 0, bp = kdb_breakpoints; i < KDB_MAXBPT; i++, bp++)
 		bp->bp_free = 1;
 
-	kdb_register_repeat("bp", kdb_bp, "[<vaddr>]",
+	kdb_register_flags("bp", kdb_bp, "[<vaddr>]",
 		"Set/Display breakpoints", 0, KDB_REPEAT_NO_ARGS);
-	kdb_register_repeat("bl", kdb_bp, "[<vaddr>]",
+	kdb_register_flags("bl", kdb_bp, "[<vaddr>]",
 		"Display breakpoints", 0, KDB_REPEAT_NO_ARGS);
 	if (arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT)
-		kdb_register_repeat("bph", kdb_bp, "[<vaddr>]",
+		kdb_register_flags("bph", kdb_bp, "[<vaddr>]",
 		"[datar [length]|dataw [length]]   Set hw brk", 0, KDB_REPEAT_NO_ARGS);
-	kdb_register_repeat("bc", kdb_bc, "<bpnum>",
+	kdb_register_flags("bc", kdb_bc, "<bpnum>",
 		"Clear Breakpoint", 0, KDB_REPEAT_NONE);
-	kdb_register_repeat("be", kdb_bc, "<bpnum>",
+	kdb_register_flags("be", kdb_bc, "<bpnum>",
 		"Enable Breakpoint", 0, KDB_REPEAT_NONE);
-	kdb_register_repeat("bd", kdb_bc, "<bpnum>",
+	kdb_register_flags("bd", kdb_bc, "<bpnum>",
 		"Disable Breakpoint", 0, KDB_REPEAT_NONE);
 
-	kdb_register_repeat("ss", kdb_ss, "",
+	kdb_register_flags("ss", kdb_ss, "",
 		"Single Step", 1, KDB_REPEAT_NO_ARGS);
-	kdb_register_repeat("ssb", kdb_ss, "",
+	kdb_register_flags("ssb", kdb_ss, "",
 		"Single step to branch/call", 0, KDB_REPEAT_NO_ARGS);
 	/*
 	 * Architecture dependent initialization.
kernel/debug/kdb/kdb_main.c +43 -43

160 lines not on this page · raw message

kernel/trace/trace_kdb.c +1 -1
index 3c5c5df..e9db346 100644
@@ -127,7 +127,7 @@ static int kdb_ftdump(int argc, const char **argv)
 
 static __init int kdb_ftrace_register(void)
 {
-	kdb_register_repeat("ftdump", kdb_ftdump, "[skip_#lines] [cpu]",
+	kdb_register_flags("ftdump", kdb_ftdump, "[skip_#lines] [cpu]",
 			    "Dump ftrace log", 0, KDB_REPEAT_NONE);
 	return 0;
 }
Signature · 1 line
1.7.10.4
#4 lore raw

PATCH 4/7 kdb: Use KDB_REPEAT_* values as flags

In reply to Anton Vorontsov

The actual values of KDB_REPEAT_* enum values and overall logic stayed the same, but we now treat the values as flags.

This makes it possible to add other flags and combine them, plus makes the code a lot simpler and shorter. But functionality-wise, there should be no changes.

---
 include/linux/kdb.h         |    4 ++--
 kernel/debug/kdb/kdb_main.c |   21 +++++++--------------
 2 files changed, 9 insertions(+), 16 deletions(-)
include/linux/kdb.h +2 -2
index 4ab0936..0a047f9 100644
@@ -34,8 +34,8 @@ extern atomic_t kdb_event;
 
 typedef enum {
 	KDB_REPEAT_NONE = 0,	/* Do not repeat this command */
-	KDB_REPEAT_NO_ARGS,	/* Repeat the command without arguments */
-	KDB_REPEAT_WITH_ARGS,	/* Repeat the command including its arguments */
+	KDB_REPEAT_NO_ARGS	= 0x1, /* Repeat the command w/o arguments */
+	KDB_REPEAT_WITH_ARGS	= 0x2, /* Repeat the command w/ its arguments */
 } kdb_cmdflags_t;
 
 typedef int (*kdb_func_t)(int, const char **);
kernel/debug/kdb/kdb_main.c +7 -14
index e9e33c1..c7d023a 100644
@@ -991,20 +991,13 @@ int kdb_parse(const char *cmdstr)
 		if (result && ignore_errors && result > KDB_CMD_GO)
 			result = 0;
 		KDB_STATE_CLEAR(CMD);
-		switch (tp->cmd_flags) {
-		case KDB_REPEAT_NONE:
-			argc = 0;
-			if (argv[0])
-				*(argv[0]) = '\0';
-			break;
-		case KDB_REPEAT_NO_ARGS:
-			argc = 1;
-			if (argv[1])
-				*(argv[1]) = '\0';
-			break;
-		case KDB_REPEAT_WITH_ARGS:
-			break;
-		}
+
+		if (tp->cmd_flags & KDB_REPEAT_WITH_ARGS)
+			return result;
+
+		argc = tp->cmd_flags & KDB_REPEAT_NO_ARGS ? 1 : 0;
+		if (argv[argc])
+			*(argv[argc]) = '\0';
 		return result;
 	}
 
Signature · 1 line
1.7.10.4
#5 lore raw

PATCH 5/7 kdb: Remove KDB_REPEAT_NONE flag

In reply to Anton Vorontsov

Since we now treat KDB_REPEAT_* as flags, there is no need to pass KDB_REPEAT_NONE. It's just the default behaviour when no flags are specified.

---
 include/linux/kdb.h         |    1 -
 kernel/debug/kdb/kdb_bp.c   |    6 ++---
 kernel/debug/kdb/kdb_main.c |   59 +++++++++++++++++++++----------------------
 kernel/trace/trace_kdb.c    |    2 +-
 4 files changed, 33 insertions(+), 35 deletions(-)
include/linux/kdb.h +0 -1
index 0a047f9..d39d41d 100644
@@ -33,7 +33,6 @@ extern atomic_t kdb_event;
 #define KDB_MAXARGS    16 /* Maximum number of arguments to a function  */
 
 typedef enum {
-	KDB_REPEAT_NONE = 0,	/* Do not repeat this command */
 	KDB_REPEAT_NO_ARGS	= 0x1, /* Repeat the command w/o arguments */
 	KDB_REPEAT_WITH_ARGS	= 0x2, /* Repeat the command w/ its arguments */
 } kdb_cmdflags_t;
kernel/debug/kdb/kdb_bp.c +3 -3
index d2cb80d..928e9e9 100644
@@ -553,11 +553,11 @@ void __init kdb_initbptab(void)
 		kdb_register_flags("bph", kdb_bp, "[<vaddr>]",
 		"[datar [length]|dataw [length]]   Set hw brk", 0, KDB_REPEAT_NO_ARGS);
 	kdb_register_flags("bc", kdb_bc, "<bpnum>",
-		"Clear Breakpoint", 0, KDB_REPEAT_NONE);
+		"Clear Breakpoint", 0, 0);
 	kdb_register_flags("be", kdb_bc, "<bpnum>",
-		"Enable Breakpoint", 0, KDB_REPEAT_NONE);
+		"Enable Breakpoint", 0, 0);
 	kdb_register_flags("bd", kdb_bc, "<bpnum>",
-		"Disable Breakpoint", 0, KDB_REPEAT_NONE);
+		"Disable Breakpoint", 0, 0);
 
 	kdb_register_flags("ss", kdb_ss, "",
 		"Single Step", 1, KDB_REPEAT_NO_ARGS);
kernel/debug/kdb/kdb_main.c +29 -30

110 lines not on this page · raw message

kernel/trace/trace_kdb.c +1 -1
index e9db346..1b68177 100644
@@ -128,7 +128,7 @@ static int kdb_ftdump(int argc, const char **argv)
 static __init int kdb_ftrace_register(void)
 {
 	kdb_register_flags("ftdump", kdb_ftdump, "[skip_#lines] [cpu]",
-			    "Dump ftrace log", 0, KDB_REPEAT_NONE);
+			    "Dump ftrace log", 0, 0);
 	return 0;
 }
 
Signature · 1 line
1.7.10.4
#6 lore raw

PATCH 6/7 kdb: Mark safe commands as KDB_SAFE and KDB_SAFE_NO_ARGS

In reply to Anton Vorontsov

This patch introduces two new flags: KDB_SAFE, denotes a safe command, and KDB_SAFE_NO_ARGS, denotes a safe command when used without arguments.

The word "safe" here used in the sense that the commands cannot be used to leak sensitive data from the memory, and cannot be used to change program flow in a predefined manner.

These flags will be used by the "kiosk" mode, i.e. when it is possible for the ordinary user to enter the KDB (or user can get the access to KDB after the crash), but we do not allow user to read dump the memory [and thus read some sensitive data].

The following commands were marked as "safe":

Clear Breakpoint
Enable Breakpoint
Disable Breakpoint
Display exception frame
Stack traceback
Display stack for process
Display stack all processes Backtrace current process on each cpu Execute cmd for each element in linked list Show environment variables
Set environment variables
Display Help Message
Switch to new cpu
Display active task list
Switch to another task
Reboot the machine immediately
List loaded kernel modules
Magic SysRq key
Display syslog buffer
Define a set of commands, down to endefcmd Send a signal to a process Summarize the system

The following commands were marked as safe when issued with no arguments:

Continue Execution

And the following commands are unsafe:

Continue Execution (with address argument) Display Memory Contents
Display Raw Memory
Display Physical Memory
Display Memory Symbolically
Modify Memory Contents
Display Registers
Modify Registers
Backtrace process given its struct task address Enter kgdb mode
Display per_cpu variables

Note that we mark "display registers" command unsafe, this is because single stepping + constantly dumping registers in string or memory functions can be used as a way to read sensitive data (it's actually trivial to exploit). Later we can do a bit better, i.e. not displaying general-purpose registers, but printing control registers.

---
 include/linux/kdb.h         |    2 ++
 kernel/debug/kdb/kdb_bp.c   |   17 +++++++++--------
 kernel/debug/kdb/kdb_main.c |   44 +++++++++++++++++++++----------------------
 kernel/trace/trace_kdb.c    |    2 +-
 4 files changed, 34 insertions(+), 31 deletions(-)
include/linux/kdb.h +2 -0
index d39d41d..36f6d09 100644
@@ -35,6 +35,8 @@ extern atomic_t kdb_event;
 typedef enum {
 	KDB_REPEAT_NO_ARGS	= 0x1, /* Repeat the command w/o arguments */
 	KDB_REPEAT_WITH_ARGS	= 0x2, /* Repeat the command w/ its arguments */
+	KDB_SAFE		= 0x4, /* Security-wise safe command */
+	KDB_SAFE_NO_ARGS	= 0x8, /* Only safe if run w/o arguments */
 } kdb_cmdflags_t;
 
 typedef int (*kdb_func_t)(int, const char **);
kernel/debug/kdb/kdb_bp.c +9 -8
index 928e9e9..b95ddf7 100644
@@ -546,23 +546,24 @@ void __init kdb_initbptab(void)
 		bp->bp_free = 1;
 
 	kdb_register_flags("bp", kdb_bp, "[<vaddr>]",
-		"Set/Display breakpoints", 0, KDB_REPEAT_NO_ARGS);
+		"Set/Display breakpoints", 0, KDB_REPEAT_NO_ARGS | KDB_SAFE);
 	kdb_register_flags("bl", kdb_bp, "[<vaddr>]",
-		"Display breakpoints", 0, KDB_REPEAT_NO_ARGS);
+		"Display breakpoints", 0, KDB_REPEAT_NO_ARGS | KDB_SAFE);
 	if (arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT)
 		kdb_register_flags("bph", kdb_bp, "[<vaddr>]",
-		"[datar [length]|dataw [length]]   Set hw brk", 0, KDB_REPEAT_NO_ARGS);
+		"[datar [length]|dataw [length]]   Set hw brk", 0,
+		KDB_REPEAT_NO_ARGS | KDB_SAFE);
 	kdb_register_flags("bc", kdb_bc, "<bpnum>",
-		"Clear Breakpoint", 0, 0);
+		"Clear Breakpoint", 0, KDB_SAFE);
 	kdb_register_flags("be", kdb_bc, "<bpnum>",
-		"Enable Breakpoint", 0, 0);
+		"Enable Breakpoint", 0, KDB_SAFE);
 	kdb_register_flags("bd", kdb_bc, "<bpnum>",
-		"Disable Breakpoint", 0, 0);
+		"Disable Breakpoint", 0, KDB_SAFE);
 
 	kdb_register_flags("ss", kdb_ss, "",
-		"Single Step", 1, KDB_REPEAT_NO_ARGS);
+		"Single Step", 1, KDB_REPEAT_NO_ARGS | KDB_SAFE);
 	kdb_register_flags("ssb", kdb_ss, "",
-		"Single step to branch/call", 0, KDB_REPEAT_NO_ARGS);
+		"Single step to branch/call", 0, KDB_REPEAT_NO_ARGS | KDB_SAFE);
 	/*
 	 * Architecture dependent initialization.
 	 */
kernel/debug/kdb/kdb_main.c +22 -22

88 lines not on this page · raw message

kernel/trace/trace_kdb.c +1 -1
index 1b68177..8353852 100644
@@ -128,7 +128,7 @@ static int kdb_ftdump(int argc, const char **argv)
 static __init int kdb_ftrace_register(void)
 {
 	kdb_register_flags("ftdump", kdb_ftdump, "[skip_#lines] [cpu]",
-			    "Dump ftrace log", 0, 0);
+			    "Dump ftrace log", 0, KDB_SAFE);
 	return 0;
 }
 
Signature · 1 line
1.7.10.4
#7 lore raw

Re: [PATCH 6/7] kdb: Mark safe commands as KDB_SAFE and KDB_SAFE_NO_ARGS

Quoted 7 lines

The following commands were marked as "safe":

Clear Breakpoint
Enable Breakpoint
Disable Breakpoint
Display exception frame
Stack traceback

This is sufficient to steal cryptographic keys in many environments. In fact you merely need two or three breakpoints and to log the order they are hit through the crypto computation.

Quoted 1 line

Display stack for process

Exposes all sorts of user data unless you mean just the call trace, in which case it's still quite useful.

Quoted 1 line

Display stack all processes

Ditto

Quoted 1 line

Send a signal to a process

Like say sending SIGSTOP to security monitoring threads or the battery manager on locked devices that rely on software battery management ?

It's an interesting idea but you need almost nothing to extract keys from a system or to subvert it.

Alan

#8 lore raw

Re: [PATCH 6/7] kdb: Mark safe commands as KDB_SAFE and KDB_SAFE_NO_ARGS

Quoted 11 lines · Alan Cox
Quoted 7 lines

The following commands were marked as "safe":

Clear Breakpoint
Enable Breakpoint
Disable Breakpoint
Display exception frame
Stack traceback

This is sufficient to steal cryptographic keys in many environments. In fact you merely need two or three breakpoints and to log the order they are hit through the crypto computation.

Neat. :-)

Breakpoints are no good then.

Quoted 8 lines
Quoted 1 line

Display stack for process

Exposes all sorts of user data unless you mean just the call trace, in which case it's still quite useful.

Quoted 1 line

Display stack all processes

Ditto

What I think is, should we just mark single stepping (as well as breakpoints) as unsafe, then it's hard to impossible to use the call trace as something meaningful?

Quoted 4 lines
Quoted 1 line

Send a signal to a process

Like say sending SIGSTOP to security monitoring threads or the battery manager on locked devices that rely on software battery management ?

Yeah, will need to zap it too.

Quoted 2 lines

It's an interesting idea but you need almost nothing to extract keys from a system or to subvert it.

Apart from the above issues?

(Now it might seem that we cut almost everything from the KDB, but KDB is not just about ordinary debugging facilities, like breakpoints or variables watch. KDB is a shell that also implements commands to query kernel about its state: e.g. in Android case there is "irqs" commands that just shows interrupts counters, that is a nice feature if used w/ KDB NMI/FIQ debugger[1], so you can see which interrupt is misbehaving. There is also a 'dmesg' command, and 'summary' and maybe others.)

Thanks!

[1] http://lwn.net/Articles/506673/

Signature · 2 lines
Anton Vorontsov
Email: [email protected]
#9 lore raw

PATCH v2 6/7 kdb: Mark safe commands as KDB_SAFE and KDB_SAFE_NO_ARGS

This patch introduces two new flags: KDB_SAFE, denotes a safe command, and KDB_SAFE_NO_ARGS, denotes a safe command when used without arguments.

The word "safe" here used in the sense that the commands cannot be used to leak sensitive data from the memory, and cannot be used to change program flow in a predefined manner.

These flags will be used by the "kiosk" mode, i.e. when it is possible for the ordinary user to enter the KDB (or user can get the access to KDB after the crash), but we do not allow user to read dump the memory [and thus read some sensitive data].

The following commands were marked as "safe":

Display exception frame
Stack traceback
Display stack for process
Display stack all processes Backtrace current process on each cpu Execute cmd for each element in linked list Show environment variables
Set environment variables
Display Help Message
Switch to new cpu
Display active task list
Switch to another task
Reboot the machine immediately
List loaded kernel modules
Magic SysRq key
Display syslog buffer
Define a set of commands, down to endefcmd Summarize the system

The following commands were marked as safe when issued with no arguments:

Continue Execution

And the following commands are unsafe:

Clear Breakpoint
Enable Breakpoint
Disable Breakpoint
Single step
Single step to branch/call
Continue Execution (with address argument) Display Memory Contents
Display Raw Memory
Display Physical Memory
Display Memory Symbolically
Modify Memory Contents
Display Registers
Modify Registers
Backtrace process given its struct task address Send a signal to a process
Enter kgdb mode
Display per_cpu variables

---

Quoted 27 lines · Anton Vorontsov
Quoted 9 lines
Quoted 5 lines

Clear Breakpoint
Enable Breakpoint
Disable Breakpoint
Display exception frame
Stack traceback

This is sufficient to steal cryptographic keys in many environments. In fact you merely need two or three breakpoints and to log the order they are hit through the crypto computation.

Neat. :-)

Breakpoints are no good then.

Quoted 8 lines
Quoted 1 line

Display stack for process

Exposes all sorts of user data unless you mean just the call trace, in which case it's still quite useful.

Quoted 1 line

Display stack all processes

Ditto

What I think is, should we just mark single stepping (as well as breakpoints) as unsafe, then it's hard to impossible to use the call trace as something meaningful?

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Send a signal to a process

Like say sending SIGSTOP to security monitoring threads or the battery manager on locked devices that rely on software battery management ?

Yeah, will need to zap it too.

So, here comes v2 of this patch. It removes the mentioned commands from the safe list: now we don't allow any code flow changes apart from 'continue'.

 include/linux/kdb.h         |    2 ++
 kernel/debug/kdb/kdb_main.c |   42 +++++++++++++++++++++---------------------
 kernel/trace/trace_kdb.c    |    2 +-
 3 files changed, 24 insertions(+), 22 deletions(-)
include/linux/kdb.h +2 -0
index d39d41d..36f6d09 100644
@@ -35,6 +35,8 @@ extern atomic_t kdb_event;
 typedef enum {
 	KDB_REPEAT_NO_ARGS	= 0x1, /* Repeat the command w/o arguments */
 	KDB_REPEAT_WITH_ARGS	= 0x2, /* Repeat the command w/ its arguments */
+	KDB_SAFE		= 0x4, /* Security-wise safe command */
+	KDB_SAFE_NO_ARGS	= 0x8, /* Only safe if run w/o arguments */
 } kdb_cmdflags_t;
 
 typedef int (*kdb_func_t)(int, const char **);
kernel/debug/kdb/kdb_main.c +21 -21

87 lines not on this page · raw message

kernel/trace/trace_kdb.c +1 -1
index 1b68177..8353852 100644
@@ -128,7 +128,7 @@ static int kdb_ftdump(int argc, const char **argv)
 static __init int kdb_ftrace_register(void)
 {
 	kdb_register_flags("ftdump", kdb_ftdump, "[skip_#lines] [cpu]",
-			    "Dump ftrace log", 0, 0);
+			    "Dump ftrace log", 0, KDB_SAFE);
 	return 0;
 }
 
Signature · 1 line
1.7.10.4
#10 lore raw

PATCH 7/7 kdb: Add kiosk mode

In reply to Anton Vorontsov

By issuing 'echo 1 > /sys/module/kdb/parameters/kiosk' or booting with kdb.kiosk=1 kernel command line option, one can still have a somewhat usable debugging facility, but not fearing that the debugger can be used to easily gain root access or dump sensitive data.

Without the kiosk mode, obtaining the root rights via KDB is a matter of a few commands, and works everywhere. For example, log in as a normal user:

cbou:~$ id
uid=1001(cbou) gid=1001(cbou) groups=1001(cbou)

Now enter KDB (for example via sysrq):

Entering kdb (current=0xffff8800065bc740, pid 920) due to Keyboard Entry kdb> ps
23 sleeping system daemon (state M) processes suppressed, use 'ps A' to see all.
Task Addr Pid Parent [*] cpu State Thread Command
0xffff8800065bc740 920 919 1 0 R 0xffff8800065bca20 *bash

0xffff880007078000 1 0 0 0 S 0xffff8800070782e0 init
[...snip...]

0xffff8800065be3c0      918        1  0    0   S  0xffff8800065be6a0  getty
0xffff8800065b9c80      919        1  0    0   S  0xffff8800065b9f60  login
0xffff8800065bc740      920      919  1    0   R  0xffff8800065bca20 *bash

All we need is the offset of cred pointers. We can look up the offset in the distro's kernel source, but it is unnecessary. We can just start dumping init's task_struct, until we see the process name:

kdb> md 0xffff880007078000
0xffff880007078000 0000000000000001 ffff88000703c000 ................
0xffff880007078010 0040210000000002 0000000000000000 .....!@.........
[...snip...]
0xffff8800070782b0 ffff8800073e0580 ffff8800073e0580 ..>.......>.....
0xffff8800070782c0 0000000074696e69 0000000000000000 init............

^ Here, 'init'. Creds are just above it, so the offset is 0x02b0.

Now we set up init's creds for our non-privileged shell:

kdb> mm 0xffff8800065bc740+0x02b0 0xffff8800073e0580 0xffff8800065bc9f0 = 0xffff8800073e0580
kdb> mm 0xffff8800065bc740+0x02b8 0xffff8800073e0580 0xffff8800065bc9f8 = 0xffff8800073e0580

And thus gaining the root:

kdb> go

cbou:~$ id
uid=0(root) gid=0(root) groups=0(root)
cbou:~$ bash
root:~#

p.s. No distro enables kdb by default (although, with a nice KDB-over-KMS feature availability, I would expect at least some would enable it), so it's not actually some kind of a major issue.

---
 include/linux/kdb.h         |    1 +
 kernel/debug/kdb/kdb_main.c |   20 +++++++++++++++++++-
 2 files changed, 20 insertions(+), 1 deletion(-)
include/linux/kdb.h +1 -0
index 36f6d09..8dad355 100644
@@ -63,6 +63,7 @@ typedef int (*kdb_func_t)(int, const char **);
 #define KDB_BADLENGTH	(-19)
 #define KDB_NOBP	(-20)
 #define KDB_BADADDR	(-21)
+#define KDB_NOPERM	(-22)
 
 /*
  * kdb_diemsg
kernel/debug/kdb/kdb_main.c +19 -1
index 1bb18e6..82973b6 100644
@@ -12,6 +12,7 @@
  */
 
 #include <linux/ctype.h>
+#include <linux/types.h>
 #include <linux/string.h>
 #include <linux/kernel.h>
 #include <linux/reboot.h>
@@ -21,6 +22,7 @@
 #include <linux/utsname.h>
 #include <linux/vmalloc.h>
 #include <linux/module.h>
+#include <linux/moduleparam.h>
 #include <linux/mm.h>
 #include <linux/init.h>
 #include <linux/kallsyms.h>
@@ -40,6 +42,12 @@
 #include <linux/slab.h>
 #include "kdb_private.h"
 
+#undef	MODULE_PARAM_PREFIX
+#define	MODULE_PARAM_PREFIX "kdb."
+
+static bool kdb_kiosk;
+module_param_named(kiosk, kdb_kiosk, bool, 0600);
+
 #define GREP_LEN 256
 char kdb_grep_string[GREP_LEN];
 int kdb_grepping_flag;
@@ -119,6 +127,7 @@ static kdbmsg_t kdbmsgs[] = {
 	KDBMSG(BADLENGTH, "Invalid length field"),
 	KDBMSG(NOBP, "No Breakpoint exists"),
 	KDBMSG(BADADDR, "Invalid address"),
+	KDBMSG(NOPERM, "Permission denied"),
 };
 #undef KDBMSG
 
@@ -986,6 +995,14 @@ int kdb_parse(const char *cmdstr)
 
 	if (i < kdb_max_commands) {
 		int result;
+
+		if (kdb_kiosk) {
+			if (!(tp->cmd_flags & (KDB_SAFE | KDB_SAFE_NO_ARGS)))
+				return KDB_NOPERM;
+			if (tp->cmd_flags & KDB_SAFE_NO_ARGS && argc > 1)
+				return KDB_NOPERM;
+		}
+
 		KDB_STATE_SET(CMD);
 		result = (*tp->cmd_func)(argc-1, (const char **)argv);
 		if (result && ignore_errors && result > KDB_CMD_GO)
@@ -1008,7 +1025,7 @@ int kdb_parse(const char *cmdstr)
 	 * obtaining the address of a variable, or the nearest symbol
 	 * to an address contained in a register.
 	 */
-	{
+	if (!kdb_kiosk) {
 		unsigned long value;
 		char *name = NULL;
 		long offset;
@@ -1024,6 +1041,7 @@ int kdb_parse(const char *cmdstr)
 		kdb_printf("\n");
 		return 0;
 	}
+	return KDB_NOPERM;
 }
 
 
Signature · 1 line
1.7.10.4
#11 lore raw
In reply to Anton Vorontsov
Quoted 7 lines · Anton Vorontsov

Hi all,

Here is a patchset that implements "kiosk" mode for KDB debugger. The mode provides reduced set of features, so that it is no longer possible to leak sensitive data via the debugger, and not possible to change program flow in a predefined manner.

Quoted 8 lines

The are two use-cases for the mode, one is evil, but another is quite legitimate.

The evil use case is used by some (ahem) phone manufaturers that want to have a debuging facilities on a production device, but still don't want you to use the debugger to gain root access. I don't like locked phones, and I would not touch this/get my hands dirty by implementing the feature just for this evil (IMHO) use case.

The point of the reduced feature set in FIQ debugger is not to prevent you from accessing your own phone, it designed to prevent others from trivially rooting your phone and reading your data. Both locked and unlocked phones run FIQ debugger. Would you carry a phone with personal data on it and KGDB enabled on the serial console?

An alternate option would be to allow userspace to write a password hash to a sysfs file, and require the password to be entered over the serial console to unlock KGDB or enable unsafe KGDB commands.

Quoted 33 lines

But there is another non-evil use case: limitting access to public devices, i.e. "kiosks", ATMs (is that too much?) or just public computers w/ guest access. I can imagine that an administrator would want to setup a kernel so that upon an oops (or a sysrq event) the kernel would enter KDB, but at the same time, he would not want to leak sensitive data from the PC by means of the debugger.

There are seven patches, the first five of them are just cleanups and preparations. I believe these five patches are good even if not considering the kiosk mode. And the rest of patches actually implement the mode -- it is pretty straightforward.

Note that we might impelement the same mode for KGDB stub, but so far we don't bother.

Thanks!

--

 include/linux/kdb.h            |   16 ++--
 kernel/debug/kdb/kdb_bp.c      |   35 ++++----
 kernel/debug/kdb/kdb_main.c    |  183 +++++++++++++++++++++-------------------
 kernel/debug/kdb/kdb_private.h |    3 +-
 kernel/trace/trace_kdb.c       |    4 +-
 5 files changed, 126 insertions(+), 115 deletions(-)

--
Anton Vorontsov
Email: [email protected]
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#12 lore raw
Quoted 9 lines · Colin Cross
Quoted 8 lines

The are two use-cases for the mode, one is evil, but another is quite legitimate.

The evil use case is used by some (ahem) phone manufaturers that want to have a debuging facilities on a production device, but still don't want you to use the debugger to gain root access. I don't like locked phones, and I would not touch this/get my hands dirty by implementing the feature just for this evil (IMHO) use case.

Quoted 5 lines

The point of the reduced feature set in FIQ debugger is not to prevent you from accessing your own phone, it designed to prevent others from trivially rooting your phone and reading your data. Both locked and unlocked phones run FIQ debugger. Would you carry a phone with personal data on it and KGDB enabled on the serial console?

Short answer: yes, I would carry such a phone. :-)

Long answer:

If someone was so interested in cracking the phone/data and so ended up with attaching serial console and attempted to use debugger techniques to gain access to my data, then thief's next step would be soldering a few wires to JTAG spots, and it will be all done in minutes. Knowledge-wise, using JTAG is even more trivial than using the debugger techniques to get to my data, you just need some HW skills.

Of course, this is unless you also have the JTAG somehow vendor-locked, but then, personally, I consider it as an utter evil. For a person who really interested in my data, attaching to a flash directly is also not a problem (the imaginary thief already soldered JTAG and if it didn't work, soldering a few more wires is not a big deal).

Maybe I'm paranoid here, but for me it is hard to believe that the reduced set was not considered as a "feature" to make life more difficult for normal users wanting their device rooted. According to copyright dates, the FIQ debugger started very early, in 2008, when most Android phone vendors were very unfriendly to hackers.

Btw, why do the lovely vendors allow me to use an external SD card on the phone? My data is not protected, but the vendors suddenly no longer care. So what changed between my data on the external SD card and in the phone itself? Nothing. Vendors care about the root access itself, not my data.

Really, I tend to care more about my naked pictures^W^W^W NDA documents I should not have taken out of the office^W^W^W^W^W loads of private data on the SD card, and less about my email password stored in phone's memory. That's because if SD card is stolen/lost, everyone can read it, any time. And if password is stolen, I have some time to change it.

All I see is a very artful way to sell shackles to naive people, and this is exactly what phone vendors do by locking their devices. If I want my data protected, I use encryption with *my* keys, I don't want to be "protected" by the ways described above.

And the KDB lock doesn't help in a way that thieves no longer need to disassemble the phone to erase all the data and resell the phone (if serial port is available outside). A guy who bought the stolen phone on eBay will never know that the phone was disassembled: only a professional can tell whether warranty seals are original. The thief would probably not even bother with restoring the seals, he can always say that the phone needed a screen replacement. (Now someone might be wondering why do I know so much about phones' black market... :-)

Still, I'm not saying that the feature is not useful at all, it is useful. But to me, it is much more useful for public PCs/ATMs, when a few keystrokes on a panicked ATM can open ATM's money depository. Or just administrator don't like when wise kids get root (yup) on classroom's PCs.

But if you say that it wasn't the case, and no one thought about the reducing the debugger in the "evil" way, so be it, I trust you. But I still don't trust the phone vendors. They showed their bad attitude in many ways towards hackers, so I think we both have quite legitimate reasons to be a little bit paranoid. :-)

Quoted 3 lines

An alternate option would be to allow userspace to write a password hash to a sysfs file, and require the password to be entered over the serial console to unlock KGDB or enable unsafe KGDB commands.

Yup, that's a very nice idea. This can be implemented by introducing "unlock" KDB command. Although, that also requires tight integration w/ user-space, i.e. on boot userland would need to supply hashing method, salt and root's password hash. The same has to be done on every password change. It is surely doable, but not sure if it is worth the efforts. Maybe, some day.

Thanks,

Signature · 2 lines
Anton Vorontsov
Email: [email protected]
#13 lore raw
Quoted 16 lines · Anton Vorontsov
Quoted 8 lines · Colin Cross
Quoted 8 lines

The are two use-cases for the mode, one is evil, but another is quite legitimate.

The evil use case is used by some (ahem) phone manufaturers that want to have a debuging facilities on a production device, but still don't want you to use the debugger to gain root access. I don't like locked phones, and I would not touch this/get my hands dirty by implementing the feature just for this evil (IMHO) use case.

Quoted 5 lines

The point of the reduced feature set in FIQ debugger is not to prevent you from accessing your own phone, it designed to prevent others from trivially rooting your phone and reading your data. Both locked and unlocked phones run FIQ debugger. Would you carry a phone with personal data on it and KGDB enabled on the serial console?

Short answer: yes, I would carry such a phone. :-)

Quoted 8 lines

Long answer:

If someone was so interested in cracking the phone/data and so ended up with attaching serial console and attempted to use debugger techniques to gain access to my data, then thief's next step would be soldering a few wires to JTAG spots, and it will be all done in minutes. Knowledge-wise, using JTAG is even more trivial than using the debugger techniques to get to my data, you just need some HW skills.

The serial console on some of these phones are accessed via the headphone jack.

Imagine an airline provides free noise cancelling headphones for flights. Those headphones are of course "smart" and covertly try to quickly capture data off of the phone's debugger interface, storing on some headphone internal flash, all without the user noticing.

So I think Colin's concerns (regardless of any paranoia about phone OEM's intentions) is reasonable.

thanks
-john

#14 lore raw
In reply to Anton Vorontsov

On Fri, Jul 27, 2012 at 6:26 PM, Anton Vorontsov <[email protected]> wrote:
<snip long rant>

Quoted 5 lines

But if you say that it wasn't the case, and no one thought about the reducing the debugger in the "evil" way, so be it, I trust you. But I still don't trust the phone vendors. They showed their bad attitude in many ways towards hackers, so I think we both have quite legitimate reasons to be a little bit paranoid. :-)

I've never seen a non-Nexus phone that used the FIQ debugger, and I believe every Nexus device has supported rooting. We leave the FIQ debugger enabled on the devices we personally carry because it allows easy debugging without compromising our data, and we leave it enabled on production devices (and leave the serial console muxed out the headphone jack) because it's more useful to end users than a blank serial console.

Quoted 16 lines
Quoted 3 lines

An alternate option would be to allow userspace to write a password hash to a sysfs file, and require the password to be entered over the serial console to unlock KGDB or enable unsafe KGDB commands.

Yup, that's a very nice idea. This can be implemented by introducing "unlock" KDB command. Although, that also requires tight integration w/ user-space, i.e. on boot userland would need to supply hashing method, salt and root's password hash. The same has to be done on every password change. It is surely doable, but not sure if it is worth the efforts. Maybe, some day.

Thanks,

--
Anton Vorontsov
Email: [email protected]