2011-02-10 14:40:06

by Serge E. Hallyn

[permalink] [raw]
Subject: [PATCH 1/1] cap_syslog: don't refuse cap_sys_admin for now (v3)

In commit ce6ada35bdf710d16582cc4869c26722547e6f11, Serge messed up
userspace backward compatibility. As Gergely pointed out, userspace
which was doing the right thing and dropping all but cap_sys_admin
before calling syslog is now breaking.

At 2.6.39 or 2.6.40, let's add a sysctl which defaults to 1. When
0, if the user has cap_sys_admin but no cap_syslog, return -EPERM.
When 1 in that case, allow. Alternatively, as David pointed out,
just leaving the behavior as below is still very useful.

Please apply.

Signed-off-by: Serge Hallyn <[email protected]>
---
kernel/printk.c | 26 ++++++++++++++++----------
1 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/kernel/printk.c b/kernel/printk.c
index 2ddbdc7..bc56386 100644
--- a/kernel/printk.c
+++ b/kernel/printk.c
@@ -274,12 +274,24 @@ int do_syslog(int type, char __user *buf, int len, bool from_file)
* at open time.
*/
if (type == SYSLOG_ACTION_OPEN || !from_file) {
- if (dmesg_restrict && !capable(CAP_SYSLOG))
- goto warn; /* switch to return -EPERM after 2.6.39 */
+ if (dmesg_restrict && !capable(CAP_SYSLOG)) {
+ /* remove after 2.6.39 */
+ if (capable(CAP_SYS_ADMIN))
+ WARN_ONCE(1, "Attempt to access syslog with CAP_SYS_ADMIN "
+ "but no CAP_SYSLOG (deprecated).\n");
+ else
+ return -EPERM;
+ }
if ((type != SYSLOG_ACTION_READ_ALL &&
type != SYSLOG_ACTION_SIZE_BUFFER) &&
- !capable(CAP_SYSLOG))
- goto warn; /* switch to return -EPERM after 2.6.39 */
+ !capable(CAP_SYSLOG)) {
+ /* remove after 2.6.39 */
+ if (capable(CAP_SYS_ADMIN))
+ WARN_ONCE(1, "Attempt to access syslog with CAP_SYS_ADMIN "
+ "but no CAP_SYSLOG (deprecated).\n");
+ else
+ return -EPERM;
+ }
}

error = security_syslog(type);
@@ -423,12 +435,6 @@ int do_syslog(int type, char __user *buf, int len, bool from_file)
}
out:
return error;
-warn:
- /* remove after 2.6.39 */
- if (capable(CAP_SYS_ADMIN))
- WARN_ONCE(1, "Attempt to access syslog with CAP_SYS_ADMIN "
- "but no CAP_SYSLOG (deprecated and denied).\n");
- return -EPERM;
}

SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)
--
1.7.2.3


2011-02-10 19:17:24

by Linus Torvalds

[permalink] [raw]
Subject: Re: [PATCH 1/1] cap_syslog: don't refuse cap_sys_admin for now (v3)

On Thu, Feb 10, 2011 at 6:40 AM, Serge E. Hallyn <[email protected]> wrote:
>
> Please apply.

Hmm. So I detest the duplication and the ugly resulting code. It was
a bit hard to follow before, now it's just nasty.

Why not make this all into some nicer helper functions instead -
something simple like the attached?

UNTESTED! I'm not going to apply it unless I get acks/tested-by's.

Linus


Attachments:
patch.diff (2.25 kB)

2011-02-10 22:42:10

by Serge E. Hallyn

[permalink] [raw]
Subject: Re: [PATCH 1/1] cap_syslog: don't refuse cap_sys_admin for now (v3)

Quoting Linus Torvalds ([email protected]):
> On Thu, Feb 10, 2011 at 6:40 AM, Serge E. Hallyn <[email protected]> wrote:
> >
> > Please apply.
>
> Hmm. So I detest the duplication and the ugly resulting code. It was
> a bit hard to follow before, now it's just nasty.
>
> Why not make this all into some nicer helper functions instead -
> something simple like the attached?
>
> UNTESTED! I'm not going to apply it unless I get acks/tested-by's.
>
> Linus

It looks correct (and much nicer indeed)

Acked-by: Serge Hallyn <[email protected]>

Setting up a box to actually test on...

thanks,
-serge

> kernel/printk.c | 54 +++++++++++++++++++++++++++++++++++-------------------
> 1 files changed, 35 insertions(+), 19 deletions(-)
>
> diff --git a/kernel/printk.c b/kernel/printk.c
> index 2ddbdc7..f51214c 100644
> --- a/kernel/printk.c
> +++ b/kernel/printk.c
> @@ -262,25 +262,47 @@ int dmesg_restrict = 1;
> int dmesg_restrict;
> #endif
>
> +static int syslog_action_restricted(int type)
> +{
> + if (dmesg_restrict)
> + return 1;
> + /* Unless restricted, we allow "read all" and "get buffer size" for everybody */
> + return type != SYSLOG_ACTION_READ_ALL && type != SYSLOG_ACTION_SIZE_BUFFER;
> +}
> +
> +static int check_syslog_permissions(int type, bool from_file)
> +{
> + /*
> + * If this is from /proc/kmsg and we've already opened it, then we've
> + * already the capabilities checks at open time.
> + */
> + if (from_file && type != SYSLOG_ACTION_OPEN)
> + return 0;
> +
> + if (syslog_action_restricted(type)) {
> + if (capable(CAP_SYSLOG))
> + return 0;
> + /* For historical reasons, accept CAP_SYS_ADMIN too, with a warning */
> + if (capable(CAP_SYS_ADMIN)) {
> + WARN_ONCE(1, "Attempt to access syslog with CAP_SYS_ADMIN "
> + "but no CAP_SYSLOG (deprecated).\n");
> + return 0;
> + }
> + return -EPERM;
> + }
> + return 0;
> +}
> +
> int do_syslog(int type, char __user *buf, int len, bool from_file)
> {
> unsigned i, j, limit, count;
> int do_clear = 0;
> char c;
> - int error = 0;
> + int error;
>
> - /*
> - * If this is from /proc/kmsg we only do the capabilities checks
> - * at open time.
> - */
> - if (type == SYSLOG_ACTION_OPEN || !from_file) {
> - if (dmesg_restrict && !capable(CAP_SYSLOG))
> - goto warn; /* switch to return -EPERM after 2.6.39 */
> - if ((type != SYSLOG_ACTION_READ_ALL &&
> - type != SYSLOG_ACTION_SIZE_BUFFER) &&
> - !capable(CAP_SYSLOG))
> - goto warn; /* switch to return -EPERM after 2.6.39 */
> - }
> + error = check_syslog_permissions(type, from_file);
> + if (error)
> + goto out;
>
> error = security_syslog(type);
> if (error)
> @@ -423,12 +445,6 @@ int do_syslog(int type, char __user *buf, int len, bool from_file)
> }
> out:
> return error;
> -warn:
> - /* remove after 2.6.39 */
> - if (capable(CAP_SYS_ADMIN))
> - WARN_ONCE(1, "Attempt to access syslog with CAP_SYS_ADMIN "
> - "but no CAP_SYSLOG (deprecated and denied).\n");
> - return -EPERM;
> }
>
> SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)

2011-02-10 22:59:37

by James Morris

[permalink] [raw]
Subject: Re: [PATCH 1/1] cap_syslog: don't refuse cap_sys_admin for now (v3)

On Thu, 10 Feb 2011, Linus Torvalds wrote:

> On Thu, Feb 10, 2011 at 6:40 AM, Serge E. Hallyn <[email protected]> wrote:
> >
> > Please apply.
>
> Hmm. So I detest the duplication and the ugly resulting code. It was
> a bit hard to follow before, now it's just nasty.
>
> Why not make this all into some nicer helper functions instead -
> something simple like the attached?
>
> UNTESTED! I'm not going to apply it unless I get acks/tested-by's.

Reviewed-by: James Morris <[email protected]>


--
James Morris
<[email protected]>

2011-02-11 16:31:21

by Serge E. Hallyn

[permalink] [raw]
Subject: Re: [PATCH 1/1] cap_syslog: don't refuse cap_sys_admin for now (v3)

Quoting Linus Torvalds ([email protected]):
> On Thu, Feb 10, 2011 at 6:40 AM, Serge E. Hallyn <[email protected]> wrote:
> >
> > Please apply.
>
> Hmm. So I detest the duplication and the ugly resulting code. It was
> a bit hard to follow before, now it's just nasty.
>
> Why not make this all into some nicer helper functions instead -
> something simple like the attached?
>
> UNTESTED! I'm not going to apply it unless I get acks/tested-by's.

Acked-by: Serge Hallyn <[email protected]>
Tested-by: Serge Hallyn <[email protected]>

I tested using klogctl with actions 10, 3 and 5. Without
dmesg_restrict set, an unprivileged program could get the
size and read-all, but not clear the buffer. With either
CAP_SYS_ADMIN or CAP_SYSLOG, it could do all three. With
dmesg_restrict set, the capabilities were needed for all
actions.

Worked perfectly.

thanks,
-serge

> Linus

> kernel/printk.c | 54 +++++++++++++++++++++++++++++++++++-------------------
> 1 files changed, 35 insertions(+), 19 deletions(-)
>
> diff --git a/kernel/printk.c b/kernel/printk.c
> index 2ddbdc7..f51214c 100644
> --- a/kernel/printk.c
> +++ b/kernel/printk.c
> @@ -262,25 +262,47 @@ int dmesg_restrict = 1;
> int dmesg_restrict;
> #endif
>
> +static int syslog_action_restricted(int type)
> +{
> + if (dmesg_restrict)
> + return 1;
> + /* Unless restricted, we allow "read all" and "get buffer size" for everybody */
> + return type != SYSLOG_ACTION_READ_ALL && type != SYSLOG_ACTION_SIZE_BUFFER;
> +}
> +
> +static int check_syslog_permissions(int type, bool from_file)
> +{
> + /*
> + * If this is from /proc/kmsg and we've already opened it, then we've
> + * already the capabilities checks at open time.
> + */
> + if (from_file && type != SYSLOG_ACTION_OPEN)
> + return 0;
> +
> + if (syslog_action_restricted(type)) {
> + if (capable(CAP_SYSLOG))
> + return 0;
> + /* For historical reasons, accept CAP_SYS_ADMIN too, with a warning */
> + if (capable(CAP_SYS_ADMIN)) {
> + WARN_ONCE(1, "Attempt to access syslog with CAP_SYS_ADMIN "
> + "but no CAP_SYSLOG (deprecated).\n");
> + return 0;
> + }
> + return -EPERM;
> + }
> + return 0;
> +}
> +
> int do_syslog(int type, char __user *buf, int len, bool from_file)
> {
> unsigned i, j, limit, count;
> int do_clear = 0;
> char c;
> - int error = 0;
> + int error;
>
> - /*
> - * If this is from /proc/kmsg we only do the capabilities checks
> - * at open time.
> - */
> - if (type == SYSLOG_ACTION_OPEN || !from_file) {
> - if (dmesg_restrict && !capable(CAP_SYSLOG))
> - goto warn; /* switch to return -EPERM after 2.6.39 */
> - if ((type != SYSLOG_ACTION_READ_ALL &&
> - type != SYSLOG_ACTION_SIZE_BUFFER) &&
> - !capable(CAP_SYSLOG))
> - goto warn; /* switch to return -EPERM after 2.6.39 */
> - }
> + error = check_syslog_permissions(type, from_file);
> + if (error)
> + goto out;
>
> error = security_syslog(type);
> if (error)
> @@ -423,12 +445,6 @@ int do_syslog(int type, char __user *buf, int len, bool from_file)
> }
> out:
> return error;
> -warn:
> - /* remove after 2.6.39 */
> - if (capable(CAP_SYS_ADMIN))
> - WARN_ONCE(1, "Attempt to access syslog with CAP_SYS_ADMIN "
> - "but no CAP_SYSLOG (deprecated and denied).\n");
> - return -EPERM;
> }
>
> SYSCALL_DEFINE3(syslog, int, type, char __user *, buf, int, len)