2019-08-07 14:45:57

by Lubashev, Igor

[permalink] [raw]
Subject: [PATCH v3 4/4] perf: Use CAP_SYS_ADMIN instead of euid==0 with ftrace

Kernel requires CAP_SYS_ADMIN instead of euid==0 to mount debugfs for ftrace.
Make perf do the same.

Signed-off-by: Igor Lubashev <[email protected]>
---
tools/perf/builtin-ftrace.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/tools/perf/builtin-ftrace.c b/tools/perf/builtin-ftrace.c
index ae1466aa3b26..d09eac8a6d57 100644
--- a/tools/perf/builtin-ftrace.c
+++ b/tools/perf/builtin-ftrace.c
@@ -13,6 +13,7 @@
#include <signal.h>
#include <fcntl.h>
#include <poll.h>
+#include <linux/capability.h>

#include "debug.h"
#include <subcmd/parse-options.h>
@@ -21,6 +22,7 @@
#include "target.h"
#include "cpumap.h"
#include "thread_map.h"
+#include "util/cap.h"
#include "util/config.h"


@@ -281,7 +283,7 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
.events = POLLIN,
};

- if (geteuid() != 0) {
+ if (!perf_cap__capable(CAP_SYS_ADMIN)) {
pr_err("ftrace only works for root!\n");
return -1;
}
--
2.7.4


2019-08-12 20:24:00

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH v3 4/4] perf: Use CAP_SYS_ADMIN instead of euid==0 with ftrace

Em Wed, Aug 07, 2019 at 10:44:17AM -0400, Igor Lubashev escreveu:
> Kernel requires CAP_SYS_ADMIN instead of euid==0 to mount debugfs for ftrace.
> Make perf do the same.
>
> Signed-off-by: Igor Lubashev <[email protected]>
> ---
> tools/perf/builtin-ftrace.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/tools/perf/builtin-ftrace.c b/tools/perf/builtin-ftrace.c
> index ae1466aa3b26..d09eac8a6d57 100644
> --- a/tools/perf/builtin-ftrace.c
> +++ b/tools/perf/builtin-ftrace.c
> @@ -13,6 +13,7 @@
> #include <signal.h>
> #include <fcntl.h>
> #include <poll.h>
> +#include <linux/capability.h>
>
> #include "debug.h"
> #include <subcmd/parse-options.h>
> @@ -21,6 +22,7 @@
> #include "target.h"
> #include "cpumap.h"
> #include "thread_map.h"
> +#include "util/cap.h"
> #include "util/config.h"
>
>
> @@ -281,7 +283,7 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
> .events = POLLIN,
> };
>
> - if (geteuid() != 0) {
> + if (!perf_cap__capable(CAP_SYS_ADMIN)) {
> pr_err("ftrace only works for root!\n");

I guess we should update the error message too?

> return -1;
> }
> --
> 2.7.4

--

- Arnaldo

2019-08-12 20:29:25

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH v3 4/4] perf: Use CAP_SYS_ADMIN instead of euid==0 with ftrace

Em Mon, Aug 12, 2019 at 05:22:51PM -0300, Arnaldo Carvalho de Melo escreveu:
> Em Wed, Aug 07, 2019 at 10:44:17AM -0400, Igor Lubashev escreveu:
> > Kernel requires CAP_SYS_ADMIN instead of euid==0 to mount debugfs for ftrace.
> > Make perf do the same.
> >
> > Signed-off-by: Igor Lubashev <[email protected]>
> > ---
> > tools/perf/builtin-ftrace.c | 4 +++-
> > 1 file changed, 3 insertions(+), 1 deletion(-)
> >
> > diff --git a/tools/perf/builtin-ftrace.c b/tools/perf/builtin-ftrace.c
> > index ae1466aa3b26..d09eac8a6d57 100644
> > --- a/tools/perf/builtin-ftrace.c
> > +++ b/tools/perf/builtin-ftrace.c
> > @@ -13,6 +13,7 @@
> > #include <signal.h>
> > #include <fcntl.h>
> > #include <poll.h>
> > +#include <linux/capability.h>
> >
> > #include "debug.h"
> > #include <subcmd/parse-options.h>
> > @@ -21,6 +22,7 @@
> > #include "target.h"
> > #include "cpumap.h"
> > #include "thread_map.h"
> > +#include "util/cap.h"
> > #include "util/config.h"
> >
> >
> > @@ -281,7 +283,7 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
> > .events = POLLIN,
> > };
> >
> > - if (geteuid() != 0) {
> > + if (!perf_cap__capable(CAP_SYS_ADMIN)) {
> > pr_err("ftrace only works for root!\n");
>
> I guess we should update the error message too?
>

I.e. I applied this as a follow up patch:

diff --git a/tools/perf/builtin-ftrace.c b/tools/perf/builtin-ftrace.c
index 01a5bb58eb04..ba8b65c2f9dc 100644
--- a/tools/perf/builtin-ftrace.c
+++ b/tools/perf/builtin-ftrace.c
@@ -284,7 +284,12 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
};

if (!perf_cap__capable(CAP_SYS_ADMIN)) {
- pr_err("ftrace only works for root!\n");
+ pr_err("ftrace only works for %s!\n",
+#ifdef HAVE_LIBCAP_SUPPORT
+ "users with the SYS_ADMIN capability"
+#else
+ "root"
+#endif
return -1;
}

2019-08-12 20:32:33

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH v3 4/4] perf: Use CAP_SYS_ADMIN instead of euid==0 with ftrace

Em Mon, Aug 12, 2019 at 05:27:06PM -0300, Arnaldo Carvalho de Melo escreveu:
> Em Mon, Aug 12, 2019 at 05:22:51PM -0300, Arnaldo Carvalho de Melo escreveu:
> > Em Wed, Aug 07, 2019 at 10:44:17AM -0400, Igor Lubashev escreveu:
> > > @@ -281,7 +283,7 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
> > > .events = POLLIN,
> > > };
> > >
> > > - if (geteuid() != 0) {
> > > + if (!perf_cap__capable(CAP_SYS_ADMIN)) {
> > > pr_err("ftrace only works for root!\n");
> >
> > I guess we should update the error message too?
> >
>
> I.e. I applied this as a follow up patch:
>
> diff --git a/tools/perf/builtin-ftrace.c b/tools/perf/builtin-ftrace.c
> index 01a5bb58eb04..ba8b65c2f9dc 100644
> --- a/tools/perf/builtin-ftrace.c
> +++ b/tools/perf/builtin-ftrace.c
> @@ -284,7 +284,12 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
> };
>
> if (!perf_cap__capable(CAP_SYS_ADMIN)) {
> - pr_err("ftrace only works for root!\n");
> + pr_err("ftrace only works for %s!\n",
> +#ifdef HAVE_LIBCAP_SUPPORT
> + "users with the SYS_ADMIN capability"
> +#else
> + "root"
> +#endif

);

:-)

> return -1;
> }
>

I've pushed the whole set to my tmp.perf/cap branch, please chec

- Arnaldo

2019-08-12 22:55:12

by Mathieu Poirier

[permalink] [raw]
Subject: Re: [PATCH v3 4/4] perf: Use CAP_SYS_ADMIN instead of euid==0 with ftrace

On Mon, 12 Aug 2019 at 14:29, Arnaldo Carvalho de Melo
<[email protected]> wrote:
>
> Em Mon, Aug 12, 2019 at 05:27:06PM -0300, Arnaldo Carvalho de Melo escreveu:
> > Em Mon, Aug 12, 2019 at 05:22:51PM -0300, Arnaldo Carvalho de Melo escreveu:
> > > Em Wed, Aug 07, 2019 at 10:44:17AM -0400, Igor Lubashev escreveu:
> > > > @@ -281,7 +283,7 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
> > > > .events = POLLIN,
> > > > };
> > > >
> > > > - if (geteuid() != 0) {
> > > > + if (!perf_cap__capable(CAP_SYS_ADMIN)) {
> > > > pr_err("ftrace only works for root!\n");
> > >
> > > I guess we should update the error message too?
> > >
> >
> > I.e. I applied this as a follow up patch:
> >
> > diff --git a/tools/perf/builtin-ftrace.c b/tools/perf/builtin-ftrace.c
> > index 01a5bb58eb04..ba8b65c2f9dc 100644
> > --- a/tools/perf/builtin-ftrace.c
> > +++ b/tools/perf/builtin-ftrace.c
> > @@ -284,7 +284,12 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
> > };
> >
> > if (!perf_cap__capable(CAP_SYS_ADMIN)) {
> > - pr_err("ftrace only works for root!\n");
> > + pr_err("ftrace only works for %s!\n",
> > +#ifdef HAVE_LIBCAP_SUPPORT
> > + "users with the SYS_ADMIN capability"
> > +#else
> > + "root"
> > +#endif
>
> );
>
> :-)
>
> > return -1;
> > }
> >
>
> I've pushed the whole set to my tmp.perf/cap branch, please chec

Please hold on before moving further - I'm getting a segmentation
fault on ARM64 that I'm still trying to figure out.

>
> - Arnaldo

2019-08-13 16:03:41

by Arnaldo Carvalho de Melo

[permalink] [raw]
Subject: Re: [PATCH v3 4/4] perf: Use CAP_SYS_ADMIN instead of euid==0 with ftrace

Em Mon, Aug 12, 2019 at 03:42:17PM -0600, Mathieu Poirier escreveu:
> On Mon, 12 Aug 2019 at 14:29, Arnaldo Carvalho de Melo
> <[email protected]> wrote:
> >
> > Em Mon, Aug 12, 2019 at 05:27:06PM -0300, Arnaldo Carvalho de Melo escreveu:
> > > Em Mon, Aug 12, 2019 at 05:22:51PM -0300, Arnaldo Carvalho de Melo escreveu:
> > > > Em Wed, Aug 07, 2019 at 10:44:17AM -0400, Igor Lubashev escreveu:
> > > > > @@ -281,7 +283,7 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
> > > > > .events = POLLIN,
> > > > > };
> > > > >
> > > > > - if (geteuid() != 0) {
> > > > > + if (!perf_cap__capable(CAP_SYS_ADMIN)) {
> > > > > pr_err("ftrace only works for root!\n");
> > > >
> > > > I guess we should update the error message too?
> > > >
> > >
> > > I.e. I applied this as a follow up patch:
> > >
> > > diff --git a/tools/perf/builtin-ftrace.c b/tools/perf/builtin-ftrace.c
> > > index 01a5bb58eb04..ba8b65c2f9dc 100644
> > > --- a/tools/perf/builtin-ftrace.c
> > > +++ b/tools/perf/builtin-ftrace.c
> > > @@ -284,7 +284,12 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
> > > };
> > >
> > > if (!perf_cap__capable(CAP_SYS_ADMIN)) {
> > > - pr_err("ftrace only works for root!\n");
> > > + pr_err("ftrace only works for %s!\n",
> > > +#ifdef HAVE_LIBCAP_SUPPORT
> > > + "users with the SYS_ADMIN capability"
> > > +#else
> > > + "root"
> > > +#endif
> >
> > );
> >
> > :-)
> >
> > > return -1;
> > > }
> > >
> >
> > I've pushed the whole set to my tmp.perf/cap branch, please chec
>
> Please hold on before moving further - I'm getting a segmentation
> fault on ARM64 that I'm still trying to figure out.

This is just sitting in my tmp branch, and in my local perf/core branch,
so that I can test it with the containers, etc.

Is this related to the following fix?

commit 3e70008a6021fffd2cd1614734603ea970773060
Author: Leo Yan <[email protected]>
Date: Fri Aug 9 18:47:52 2019 +0800

perf trace: Fix segmentation fault when access syscall info on arm64

'perf trace' reports the segmentation fault as below on Arm64:

# perf trace -e string -e augmented_raw_syscalls.c
LLVM: dumping tools/perf/examples/bpf/augmented_raw_syscalls.o
perf: Segmentation fault
Obtained 12 stack frames.
perf(sighandler_dump_stack+0x47) [0xaaaaac96ac87]
linux-vdso.so.1(+0x5b7) [0xffffadbeb5b7]
/lib/aarch64-linux-gnu/libc.so.6(strlen+0x10) [0xfffface7d5d0]
/lib/aarch64-linux-gnu/libc.so.6(_IO_vfprintf+0x1ac7) [0xfffface49f97]
/lib/aarch64-linux-gnu/libc.so.6(__vsnprintf_chk+0xc7) [0xffffacedfbe7]
perf(scnprintf+0x97) [0xaaaaac9ca3ff]
perf(+0x997bb) [0xaaaaac8e37bb]
perf(cmd_trace+0x28e7) [0xaaaaac8ec09f]
perf(+0xd4a13) [0xaaaaac91ea13]
perf(main+0x62f) [0xaaaaac8a147f]
/lib/aarch64-linux-gnu/libc.so.6(__libc_start_main+0xe3) [0xfffface22d23]
perf(+0x57723) [0xaaaaac8a1723]
Segmentation fault

This issue is introduced by commit 30a910d7d3e0 ("perf trace:
Preallocate the syscall table"), it allocates trace->syscalls.table[]
array and the element count is 'trace->sctbl->syscalls.nr_entries'; but
on Arm64, the system call number is not continuously used; e.g. the
syscall maximum id is 436 but the real entries is only 281.


2019-08-13 16:49:49

by Mathieu Poirier

[permalink] [raw]
Subject: Re: [PATCH v3 4/4] perf: Use CAP_SYS_ADMIN instead of euid==0 with ftrace

Hi Arnaldo,

On Tue, 13 Aug 2019 at 07:23, Arnaldo Carvalho de Melo
<[email protected]> wrote:
>
> Em Mon, Aug 12, 2019 at 03:42:17PM -0600, Mathieu Poirier escreveu:
> > On Mon, 12 Aug 2019 at 14:29, Arnaldo Carvalho de Melo
> > <[email protected]> wrote:
> > >
> > > Em Mon, Aug 12, 2019 at 05:27:06PM -0300, Arnaldo Carvalho de Melo escreveu:
> > > > Em Mon, Aug 12, 2019 at 05:22:51PM -0300, Arnaldo Carvalho de Melo escreveu:
> > > > > Em Wed, Aug 07, 2019 at 10:44:17AM -0400, Igor Lubashev escreveu:
> > > > > > @@ -281,7 +283,7 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
> > > > > > .events = POLLIN,
> > > > > > };
> > > > > >
> > > > > > - if (geteuid() != 0) {
> > > > > > + if (!perf_cap__capable(CAP_SYS_ADMIN)) {
> > > > > > pr_err("ftrace only works for root!\n");
> > > > >
> > > > > I guess we should update the error message too?
> > > > >
> > > >
> > > > I.e. I applied this as a follow up patch:
> > > >
> > > > diff --git a/tools/perf/builtin-ftrace.c b/tools/perf/builtin-ftrace.c
> > > > index 01a5bb58eb04..ba8b65c2f9dc 100644
> > > > --- a/tools/perf/builtin-ftrace.c
> > > > +++ b/tools/perf/builtin-ftrace.c
> > > > @@ -284,7 +284,12 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
> > > > };
> > > >
> > > > if (!perf_cap__capable(CAP_SYS_ADMIN)) {
> > > > - pr_err("ftrace only works for root!\n");
> > > > + pr_err("ftrace only works for %s!\n",
> > > > +#ifdef HAVE_LIBCAP_SUPPORT
> > > > + "users with the SYS_ADMIN capability"
> > > > +#else
> > > > + "root"
> > > > +#endif
> > >
> > > );
> > >
> > > :-)
> > >
> > > > return -1;
> > > > }
> > > >
> > >
> > > I've pushed the whole set to my tmp.perf/cap branch, please chec
> >
> > Please hold on before moving further - I'm getting a segmentation
> > fault on ARM64 that I'm still trying to figure out.
>
> This is just sitting in my tmp branch, and in my local perf/core branch,
> so that I can test it with the containers, etc.
>
> Is this related to the following fix?

That is the first thing I thought about but no, it has nothing to do
with it. Patch 3/4 is where the problem shows up. The code in the
patch is fine, it is the repercussion it has on other part that needs
to be investigated.

Right now I see that kmap->ref_reloc_sym is NULL here [1] when tracing
with anything else than the 'u' option. I am currently investigating
the problem.

Igor, please see if you can reproduce on QEMU or an ARM64 based platform.

[1] https://elixir.bootlin.com/linux/v5.3-rc4/source/tools/perf/util/event.c#L945

>
> commit 3e70008a6021fffd2cd1614734603ea970773060
> Author: Leo Yan <[email protected]>
> Date: Fri Aug 9 18:47:52 2019 +0800
>
> perf trace: Fix segmentation fault when access syscall info on arm64
>
> 'perf trace' reports the segmentation fault as below on Arm64:
>
> # perf trace -e string -e augmented_raw_syscalls.c
> LLVM: dumping tools/perf/examples/bpf/augmented_raw_syscalls.o
> perf: Segmentation fault
> Obtained 12 stack frames.
> perf(sighandler_dump_stack+0x47) [0xaaaaac96ac87]
> linux-vdso.so.1(+0x5b7) [0xffffadbeb5b7]
> /lib/aarch64-linux-gnu/libc.so.6(strlen+0x10) [0xfffface7d5d0]
> /lib/aarch64-linux-gnu/libc.so.6(_IO_vfprintf+0x1ac7) [0xfffface49f97]
> /lib/aarch64-linux-gnu/libc.so.6(__vsnprintf_chk+0xc7) [0xffffacedfbe7]
> perf(scnprintf+0x97) [0xaaaaac9ca3ff]
> perf(+0x997bb) [0xaaaaac8e37bb]
> perf(cmd_trace+0x28e7) [0xaaaaac8ec09f]
> perf(+0xd4a13) [0xaaaaac91ea13]
> perf(main+0x62f) [0xaaaaac8a147f]
> /lib/aarch64-linux-gnu/libc.so.6(__libc_start_main+0xe3) [0xfffface22d23]
> perf(+0x57723) [0xaaaaac8a1723]
> Segmentation fault
>
> This issue is introduced by commit 30a910d7d3e0 ("perf trace:
> Preallocate the syscall table"), it allocates trace->syscalls.table[]
> array and the element count is 'trace->sctbl->syscalls.nr_entries'; but
> on Arm64, the system call number is not continuously used; e.g. the
> syscall maximum id is 436 but the real entries is only 281.
>
>

Subject: [tip:perf/core] perf ftrace: Use CAP_SYS_ADMIN instead of euid==0

Commit-ID: c766f3df635de14295e410c6dd5410bc416c24a0
Gitweb: https://git.kernel.org/tip/c766f3df635de14295e410c6dd5410bc416c24a0
Author: Igor Lubashev <[email protected]>
AuthorDate: Wed, 7 Aug 2019 10:44:17 -0400
Committer: Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Wed, 14 Aug 2019 10:59:59 -0300

perf ftrace: Use CAP_SYS_ADMIN instead of euid==0

The kernel requires CAP_SYS_ADMIN instead of euid==0 to mount debugfs
for ftrace. Make perf do the same.

Signed-off-by: Igor Lubashev <[email protected]>
Acked-by: Jiri Olsa <[email protected]>
Cc: Alexander Shishkin <[email protected]>
Cc: Alexey Budankov <[email protected]>
Cc: James Morris <[email protected]>
Cc: Mathieu Poirier <[email protected]>
Cc: Namhyung Kim <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Suzuki Poulouse <[email protected]>
Cc: [email protected]
Link: http://lkml.kernel.org/r/bd8763b72ed4d58d0b42d44fbc7eb474d32e53a3.1565188228.git.ilubashe@akamai.com
Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
---
tools/perf/builtin-ftrace.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/tools/perf/builtin-ftrace.c b/tools/perf/builtin-ftrace.c
index 20d4c0ce8b53..01a5bb58eb04 100644
--- a/tools/perf/builtin-ftrace.c
+++ b/tools/perf/builtin-ftrace.c
@@ -13,6 +13,7 @@
#include <signal.h>
#include <fcntl.h>
#include <poll.h>
+#include <linux/capability.h>

#include "debug.h"
#include <subcmd/parse-options.h>
@@ -21,6 +22,7 @@
#include "target.h"
#include "cpumap.h"
#include "thread_map.h"
+#include "util/cap.h"
#include "util/config.h"


@@ -281,7 +283,7 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
.events = POLLIN,
};

- if (geteuid() != 0) {
+ if (!perf_cap__capable(CAP_SYS_ADMIN)) {
pr_err("ftrace only works for root!\n");
return -1;
}