2005-03-15 18:37:22

by Artem Frolov

[permalink] [raw]
Subject: Taking strlen of buffers copied from userspace

Hello,

I am in the process of testing static defect analyzer on a Linux
kernel source code (see disclosure below).

I found some potential array bounds violations. The pattern is as
follows: bytes are copied from the user space and then buffer is
accessed on index strlen(buf)-1. This is a defect if user data start
from 0. So the question is: can we make any assumptions what data may
be received from the user or it could be arbitrary?

For example, in ./drivers/block/cciss.c, function cciss_proc_write
(line numbers are taken form 2.6.11.3):
....
293 if (count > sizeof(cmd)-1) return -EINVAL;
294 if (copy_from_user(cmd, buffer, count)) return -EFAULT;
295 cmd[count] = '\0';
296 len = strlen(cmd); // above 3 lines ensure safety
297 if (cmd[len-1] == '\n')
298 cmd[--len] = '\0';
.....

Another example is arch/i386/kernel/cpu/mtrr/if.c, function mtrr_write:
....
107 if (copy_from_user(line, buf, len - 1))
108 return -EFAULT;
109 ptr = line + strlen(line) - 1;
110 if (*ptr == '\n')
111 *ptr = '\0';
....


Full disclosure: I am working for Klocwork (http://www.klocwork.com/),
which is a vendor of commercial closed-source proprietary products,
static analyzer for C/C++ is part of its products

Best regards
--
Artem Frolov
Senior software engineer
Klocwork inc


2005-03-15 23:59:00

by Robert Hancock

[permalink] [raw]
Subject: Re: Taking strlen of buffers copied from userspace

Artem Frolov wrote:
> Hello,
>
> I am in the process of testing static defect analyzer on a Linux
> kernel source code (see disclosure below).
>
> I found some potential array bounds violations. The pattern is as
> follows: bytes are copied from the user space and then buffer is
> accessed on index strlen(buf)-1. This is a defect if user data start
> from 0. So the question is: can we make any assumptions what data may
> be received from the user or it could be arbitrary?

In general I don't think any such assumptions should be made. In the
case of the two below I'm assuming that root access is required to write
those files, preventing any serious security hole, but it shouldn't
really be permitted to corrupt kernel memory like this, as would likely
happen if somebody wrote some data that contained a null as the first
character.

>
> For example, in ./drivers/block/cciss.c, function cciss_proc_write
> (line numbers are taken form 2.6.11.3):
> ....
> 293 if (count > sizeof(cmd)-1) return -EINVAL;
> 294 if (copy_from_user(cmd, buffer, count)) return -EFAULT;
> 295 cmd[count] = '\0';
> 296 len = strlen(cmd); // above 3 lines ensure safety
> 297 if (cmd[len-1] == '\n')
> 298 cmd[--len] = '\0';
> .....
>
> Another example is arch/i386/kernel/cpu/mtrr/if.c, function mtrr_write:
> ....
> 107 if (copy_from_user(line, buf, len - 1))
> 108 return -EFAULT;
> 109 ptr = line + strlen(line) - 1;
> 110 if (*ptr == '\n')
> 111 *ptr = '\0';
> ....
>

This one is also unsafe if somebody writes some data which is not
null-terminated (assuming that that's possible), since strlen will run
off the end of the buffer. The first example doesn't have that problem.

--
Robert Hancock Saskatoon, SK, Canada
To email, remove "nospam" from [email protected]
Home Page: http://www.roberthancock.com/

2005-03-16 04:21:44

by Randy.Dunlap

[permalink] [raw]
Subject: Re: Taking strlen of buffers copied from userspace

Robert Hancock wrote:
> Artem Frolov wrote:
>
>> Hello,
>>
>> I am in the process of testing static defect analyzer on a Linux
>> kernel source code (see disclosure below).
>>
>> I found some potential array bounds violations. The pattern is as
>> follows: bytes are copied from the user space and then buffer is
>> accessed on index strlen(buf)-1. This is a defect if user data start
>> from 0. So the question is: can we make any assumptions what data may
>> be received from the user or it could be arbitrary?
>
>
> In general I don't think any such assumptions should be made. In the
> case of the two below I'm assuming that root access is required to write
> those files, preventing any serious security hole, but it shouldn't
> really be permitted to corrupt kernel memory like this, as would likely
> happen if somebody wrote some data that contained a null as the first
> character.
>
>>
>> For example, in ./drivers/block/cciss.c, function cciss_proc_write
>> (line numbers are taken form 2.6.11.3):
>> ....
>> 293 if (count > sizeof(cmd)-1) return -EINVAL;
>> 294 if (copy_from_user(cmd, buffer, count)) return -EFAULT;
>> 295 cmd[count] = '\0';
>> 296 len = strlen(cmd); // above 3 lines ensure safety
>> 297 if (cmd[len-1] == '\n')
>> 298 cmd[--len] = '\0';
>> .....
>>
>> Another example is arch/i386/kernel/cpu/mtrr/if.c, function mtrr_write:
>> ....
>> 107 if (copy_from_user(line, buf, len - 1))
>> 108 return -EFAULT;
>> 109 ptr = line + strlen(line) - 1;
>> 110 if (*ptr == '\n')
>> 111 *ptr = '\0';
>> ....
>>
>
> This one is also unsafe if somebody writes some data which is not
> null-terminated (assuming that that's possible), since strlen will run
> off the end of the buffer. The first example doesn't have that problem.

The latter one does (before the listed code):

memset(line, 0, LINE_SIZE);
if (len > LINE_SIZE)
len = LINE_SIZE;
if (copy_from_user(line, buf, len - 1))
return -EFAULT;

so isn't line[LINE_SIZE - 1] always 0 ?

--
~Randy

2005-03-16 05:25:02

by Robert Hancock

[permalink] [raw]
Subject: Re: Taking strlen of buffers copied from userspace

Randy.Dunlap wrote:
> The latter one does (before the listed code):
>
> memset(line, 0, LINE_SIZE);
> if (len > LINE_SIZE)
> len = LINE_SIZE;
> if (copy_from_user(line, buf, len - 1))
> return -EFAULT;
>
> so isn't line[LINE_SIZE - 1] always 0 ?

In that case, yes (I hadn't looked at the surrounding code). Rather an
odd way of doing it, but shouldn't have that problem. Could still be
subject to problems if buf contains a null at the first character,
unless they're somehow preventing that too..

--
Robert Hancock Saskatoon, SK, Canada
To email, remove "nospam" from [email protected]
Home Page: http://www.roberthancock.com/

2005-03-16 05:35:19

by Randy.Dunlap

[permalink] [raw]
Subject: Re: Taking strlen of buffers copied from userspace

Robert Hancock wrote:
> Randy.Dunlap wrote:
>
>> The latter one does (before the listed code):
>>
>> memset(line, 0, LINE_SIZE);
>> if (len > LINE_SIZE)
>> len = LINE_SIZE;
>> if (copy_from_user(line, buf, len - 1))
>> return -EFAULT;
>>
>> so isn't line[LINE_SIZE - 1] always 0 ?
>
>
> In that case, yes (I hadn't looked at the surrounding code). Rather an
> odd way of doing it, but shouldn't have that problem. Could still be
> subject to problems if buf contains a null at the first character,
> unless they're somehow preventing that too..

Yes, that's still a problem.

--
~Randy

2005-03-16 22:37:11

by Randy.Dunlap

[permalink] [raw]
Subject: [PATCH] Taking strlen of buffers copied from userspace

Artem Frolov wrote:
> Hello,
>
> I am in the process of testing static defect analyzer on a Linux
> kernel source code (see disclosure below).
>
> I found some potential array bounds violations. The pattern is as
> follows: bytes are copied from the user space and then buffer is
> accessed on index strlen(buf)-1. This is a defect if user data start
> from 0. So the question is: can we make any assumptions what data may
> be received from the user or it could be arbitrary?

Both are potential problems for someone with CAP_SYS_ADMIN
capabilties. Attached are patches for them.


> Full disclosure: I am working for Klocwork (http://www.klocwork.com/),
> which is a vendor of commercial closed-source proprietary products,
> static analyzer for C/C++ is part of its products


--
~Randy


Attachments:
mtrr_strlen_v2.patch (1.10 kB)
cciss_strlen.patch (748.00 B)
Download all attachments