Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH 2/3] tracing: make sure the parsed string always terminates with '\0'
       [not found] <1515491748-25926-1-git-send-email-changbin.du@intel.com>
@ 2018-01-09  9:55 ` changbin.du
  2018-01-09 23:02   ` Steven Rostedt
  0 siblings, 1 reply; 5+ messages in thread
From: changbin.du @ 2018-01-09  9:55 UTC (permalink / raw)
  To: rostedt
  Cc: jolsa, peterz, mingo, alexander.shishkin, linux-kernel,
	linux-perf-users, Changbin Du, stable

From: Changbin Du <changbin.du@intel.com>

The parser parse every string into parser.buffer. And some of the callers
assume that parser.buffer contains a C string. So it is dangerous that the
parser returns a unterminated string. The userspace can leverage this to
attack the kernel.

Signed-off-by: Changbin Du <changbin.du@intel.com>
Cc: stable@vger.kernel.org
---
 kernel/trace/trace.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 18526a1..e1baca0 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -530,8 +530,6 @@ int trace_pid_write(struct trace_pid_list *filtered_pids,
 		ubuf += ret;
 		cnt -= ret;
 
-		parser.buffer[parser.idx] = 0;
-
 		ret = -EINVAL;
 		if (kstrtoul(parser.buffer, 0, &val))
 			break;
@@ -1253,7 +1251,7 @@ int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
 
 	/* read the non-space input */
 	while (cnt && !is_space_or_zero(ch)) {
-		if (parser->idx < parser->size - 1)
+		if (parser->idx < parser->size - 2)
 			parser->buffer[parser->idx++] = ch;
 		else {
 			ret = -EINVAL;
@@ -1270,9 +1268,11 @@ int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
 	if (is_space_or_zero(ch)) {
 		parser->buffer[parser->idx] = 0;
 		parser->cont = false;
-	} else if (parser->idx < parser->size - 1) {
+	} else if (parser->idx < parser->size - 2) {
 		parser->cont = true;
 		parser->buffer[parser->idx++] = ch;
+		/* Make sure the parsed string always terminates with '\0'. */
+		parser->buffer[parser->idx] = 0;
 	} else {
 		ret = -EINVAL;
 		goto out;
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 2/3] tracing: make sure the parsed string always terminates with '\0'
  2018-01-09  9:55 ` [PATCH 2/3] tracing: make sure the parsed string always terminates with '\0' changbin.du
@ 2018-01-09 23:02   ` Steven Rostedt
  2018-01-10  3:02     ` Du, Changbin
  0 siblings, 1 reply; 5+ messages in thread
From: Steven Rostedt @ 2018-01-09 23:02 UTC (permalink / raw)
  To: changbin.du
  Cc: jolsa, peterz, mingo, alexander.shishkin, linux-kernel,
	linux-perf-users, stable

On Tue,  9 Jan 2018 17:55:47 +0800
changbin.du@intel.com wrote:

> From: Changbin Du <changbin.du@intel.com>
> 
> The parser parse every string into parser.buffer. And some of the callers
> assume that parser.buffer contains a C string. So it is dangerous that the
> parser returns a unterminated string. The userspace can leverage this to
> attack the kernel.

Is this only a bug if we apply your first patch?

-- Steve

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 2/3] tracing: make sure the parsed string always terminates with '\0'
  2018-01-09 23:02   ` Steven Rostedt
@ 2018-01-10  3:02     ` Du, Changbin
  2018-01-10  4:10       ` Steven Rostedt
  0 siblings, 1 reply; 5+ messages in thread
From: Du, Changbin @ 2018-01-10  3:02 UTC (permalink / raw)
  To: Steven Rostedt
  Cc: changbin.du, jolsa, peterz, mingo, alexander.shishkin,
	linux-kernel, linux-perf-users, stable

On Tue, Jan 09, 2018 at 06:02:58PM -0500, Steven Rostedt wrote:
> On Tue,  9 Jan 2018 17:55:47 +0800
> changbin.du@intel.com wrote:
> 
> > From: Changbin Du <changbin.du@intel.com>
> > 
> > The parser parse every string into parser.buffer. And some of the callers
> > assume that parser.buffer contains a C string. So it is dangerous that the
> > parser returns a unterminated string. The userspace can leverage this to
> > attack the kernel.
> 
> Is this only a bug if we apply your first patch?
>
I don't think so. Seems it is there already.
 
> -- Steve
> 

-- 
Thanks,
Changbin Du

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 2/3] tracing: make sure the parsed string always terminates with '\0'
  2018-01-10  3:02     ` Du, Changbin
@ 2018-01-10  4:10       ` Steven Rostedt
  2018-01-15 10:49         ` Du, Changbin
  0 siblings, 1 reply; 5+ messages in thread
From: Steven Rostedt @ 2018-01-10  4:10 UTC (permalink / raw)
  To: Du, Changbin
  Cc: jolsa, peterz, mingo, alexander.shishkin, linux-kernel,
	linux-perf-users, stable

On Wed, 10 Jan 2018 11:02:06 +0800
"Du, Changbin" <changbin.du@intel.com> wrote:

> On Tue, Jan 09, 2018 at 06:02:58PM -0500, Steven Rostedt wrote:
> > On Tue,  9 Jan 2018 17:55:47 +0800
> > changbin.du@intel.com wrote:
> >   
> > > From: Changbin Du <changbin.du@intel.com>
> > > 
> > > The parser parse every string into parser.buffer. And some of the callers
> > > assume that parser.buffer contains a C string. So it is dangerous that the
> > > parser returns a unterminated string. The userspace can leverage this to
> > > attack the kernel.  
> > 
> > Is this only a bug if we apply your first patch?
> >  
> I don't think so. Seems it is there already.
>  

OK. I'll have to take a deeper look into this so that I completely
understand the problem and your solution. I'm currently traveling and
may not get to do that this week. Please ping me next week if you don't
hear back from me on this issue.

Thanks!

-- Steve

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 2/3] tracing: make sure the parsed string always terminates with '\0'
  2018-01-10  4:10       ` Steven Rostedt
@ 2018-01-15 10:49         ` Du, Changbin
  0 siblings, 0 replies; 5+ messages in thread
From: Du, Changbin @ 2018-01-15 10:49 UTC (permalink / raw)
  To: Steven Rostedt
  Cc: Du, Changbin, jolsa, peterz, mingo, alexander.shishkin,
	linux-kernel, linux-perf-users, stable

On Tue, Jan 09, 2018 at 11:10:22PM -0500, Steven Rostedt wrote:
> On Wed, 10 Jan 2018 11:02:06 +0800
> "Du, Changbin" <changbin.du@intel.com> wrote:
> 
> > On Tue, Jan 09, 2018 at 06:02:58PM -0500, Steven Rostedt wrote:
> > > On Tue,  9 Jan 2018 17:55:47 +0800
> > > changbin.du@intel.com wrote:
> > >   
> > > > From: Changbin Du <changbin.du@intel.com>
> > > > 
> > > > The parser parse every string into parser.buffer. And some of the callers
> > > > assume that parser.buffer contains a C string. So it is dangerous that the
> > > > parser returns a unterminated string. The userspace can leverage this to
> > > > attack the kernel.  
> > > 
> > > Is this only a bug if we apply your first patch?
> > >  
> > I don't think so. Seems it is there already.
> >  
> 
> OK. I'll have to take a deeper look into this so that I completely
> understand the problem and your solution. I'm currently traveling and
> may not get to do that this week. Please ping me next week if you don't
> hear back from me on this issue.
> 
> Thanks!
> 
> -- Steve

I checked every trace_get_user() clients again and found it is not an issue in
current kernel. The client has checked trace_parser_cont() before using parsed
string or append '\0'.

I still want to make the parser returns a '\0' terminated string. Then we don't
require the clients append it. I think this would be better since we are dealing
with strings.

-- 
Thanks,
Changbin Du

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2018-01-15 10:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <1515491748-25926-1-git-send-email-changbin.du@intel.com>
2018-01-09  9:55 ` [PATCH 2/3] tracing: make sure the parsed string always terminates with '\0' changbin.du
2018-01-09 23:02   ` Steven Rostedt
2018-01-10  3:02     ` Du, Changbin
2018-01-10  4:10       ` Steven Rostedt
2018-01-15 10:49         ` Du, Changbin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox