Skip to content

Commit 11870d7

Browse files
mhiramatacmel
authored andcommitted
perf symbols: Introduce filename__readable to check readability
Introduce filename__readable to check readability by opening the file directly. Since the access(R_OK) just checks the readability based on real UID/GID, it is ignored that the effective UID/GID and capabilities for some special file (e.g. /proc/kcore). filename__readable() directly opens given file with O_RDONLY so that the kernel checks it by effective UID/GID and capabilities. Signed-off-by: Masami Hiramatsu <[email protected]> Acked-by: Namhyung Kim <[email protected]> Cc: Ananth N Mavinakayanahalli <[email protected]> Cc: Brendan Gregg <[email protected]> Cc: Hemant Kumar <[email protected]> Cc: Peter Zijlstra <[email protected]> Link: http://lkml.kernel.org/r/20160528151513.16098.97576.stgit@devbox Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent dcd1e2a commit 11870d7

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

tools/perf/util/symbol.c

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,6 +1641,20 @@ static int find_matching_kcore(struct map *map, char *dir, size_t dir_sz)
16411641
return ret;
16421642
}
16431643

1644+
/*
1645+
* Use open(O_RDONLY) to check readability directly instead of access(R_OK)
1646+
* since access(R_OK) only checks with real UID/GID but open() use effective
1647+
* UID/GID and actual capabilities (e.g. /proc/kcore requires CAP_SYS_RAWIO).
1648+
*/
1649+
static bool filename__readable(const char *file)
1650+
{
1651+
int fd = open(file, O_RDONLY);
1652+
if (fd < 0)
1653+
return false;
1654+
close(fd);
1655+
return true;
1656+
}
1657+
16441658
static char *dso__find_kallsyms(struct dso *dso, struct map *map)
16451659
{
16461660
u8 host_build_id[BUILD_ID_SIZE];
@@ -1668,7 +1682,6 @@ static char *dso__find_kallsyms(struct dso *dso, struct map *map)
16681682
/* Use /proc/kallsyms if possible */
16691683
if (is_host) {
16701684
DIR *d;
1671-
int fd;
16721685

16731686
/* If no cached kcore go with /proc/kallsyms */
16741687
d = opendir(path);
@@ -1677,16 +1690,15 @@ static char *dso__find_kallsyms(struct dso *dso, struct map *map)
16771690
closedir(d);
16781691

16791692
/*
1680-
* Do not check the build-id cache, until we know we cannot use
1681-
* /proc/kcore.
1693+
* Do not check the build-id cache, unless we know we cannot use
1694+
* /proc/kcore or module maps don't match to /proc/kallsyms.
1695+
* To check readability of /proc/kcore, do not use access(R_OK)
1696+
* since /proc/kcore requires CAP_SYS_RAWIO to read and access
1697+
* can't check it.
16821698
*/
1683-
fd = open("/proc/kcore", O_RDONLY);
1684-
if (fd != -1) {
1685-
close(fd);
1686-
/* If module maps match go with /proc/kallsyms */
1687-
if (!validate_kcore_addresses("/proc/kallsyms", map))
1688-
goto proc_kallsyms;
1689-
}
1699+
if (filename__readable("/proc/kcore") &&
1700+
!validate_kcore_addresses("/proc/kallsyms", map))
1701+
goto proc_kallsyms;
16901702

16911703
/* Find kallsyms in build-id cache with kcore */
16921704
if (!find_matching_kcore(map, path, sizeof(path)))

0 commit comments

Comments
 (0)