|
|
5f7b57 |
From 16b2ebdb678b7475bacb80dd59e949055d3f856c Mon Sep 17 00:00:00 2001
|
|
|
5f7b57 |
From: Jakub Filak <jfilak@redhat.com>
|
|
|
5f7b57 |
Date: Fri, 24 Apr 2015 11:42:18 +0200
|
|
|
5f7b57 |
Subject: [LIBREPORT PATCH] lib: add alternative dd functions accepting fds
|
|
|
5f7b57 |
|
|
|
5f7b57 |
Florian Weimer <fweimer@redhat.com>:
|
|
|
5f7b57 |
|
|
|
5f7b57 |
dump_dir_accessible_by_uid() is fundamentally insecure because it
|
|
|
5f7b57 |
opens up a classic time-of-check-time-of-use race between this
|
|
|
5f7b57 |
function and and dd_opendir(). At least re-checking after
|
|
|
5f7b57 |
dd_opendir() with the stored file descriptor is needed.
|
|
|
5f7b57 |
|
|
|
5f7b57 |
Related: #1214745
|
|
|
5f7b57 |
|
|
|
5f7b57 |
Signed-off-by: Jakub Filak <jfilak@redhat.com>
|
|
|
5f7b57 |
---
|
|
|
5f7b57 |
src/include/dump_dir.h | 8 +++++
|
|
|
5f7b57 |
src/lib/dump_dir.c | 82 +++++++++++++++++++++++++++++++++++++++++---------
|
|
|
5f7b57 |
2 files changed, 75 insertions(+), 15 deletions(-)
|
|
|
5f7b57 |
|
|
|
5f7b57 |
diff --git a/src/include/dump_dir.h b/src/include/dump_dir.h
|
|
|
5f7b57 |
index d675362..07b119a 100644
|
|
|
5f7b57 |
--- a/src/include/dump_dir.h
|
|
|
5f7b57 |
+++ b/src/include/dump_dir.h
|
|
|
5f7b57 |
@@ -68,7 +68,13 @@ struct dump_dir {
|
|
|
5f7b57 |
|
|
|
5f7b57 |
void dd_close(struct dump_dir *dd);
|
|
|
5f7b57 |
|
|
|
5f7b57 |
+/* Opens the given path and returns the resulting file descriptor.
|
|
|
5f7b57 |
+ */
|
|
|
5f7b57 |
+int dd_openfd(const char *dir);
|
|
|
5f7b57 |
struct dump_dir *dd_opendir(const char *dir, int flags);
|
|
|
5f7b57 |
+/* Skips dd_openfd(dir) and uses the given file descriptor instead
|
|
|
5f7b57 |
+ */
|
|
|
5f7b57 |
+struct dump_dir *dd_fdopendir(int dir_fd, const char *dir, int flags);
|
|
|
5f7b57 |
struct dump_dir *dd_create_skeleton(const char *dir, uid_t uid, mode_t mode, int flags);
|
|
|
5f7b57 |
int dd_reset_ownership(struct dump_dir *dd);
|
|
|
5f7b57 |
/* Pass uid = (uid_t)-1L to disable chown'ing of newly created files
|
|
|
5f7b57 |
@@ -147,6 +153,7 @@ void delete_dump_dir(const char *dirname);
|
|
|
5f7b57 |
* Returns non zero if dump dir is accessible otherwise return 0 value.
|
|
|
5f7b57 |
*/
|
|
|
5f7b57 |
int dump_dir_accessible_by_uid(const char *dirname, uid_t uid);
|
|
|
5f7b57 |
+int fdump_dir_accessible_by_uid(int dir_fd, uid_t uid);
|
|
|
5f7b57 |
|
|
|
5f7b57 |
enum {
|
|
|
5f7b57 |
DD_STAT_ACCESSIBLE_BY_UID = 1,
|
|
|
5f7b57 |
@@ -161,6 +168,7 @@ enum {
|
|
|
5f7b57 |
* Returns negative number if error occurred otherwise returns 0 or positive number.
|
|
|
5f7b57 |
*/
|
|
|
5f7b57 |
int dump_dir_stat_for_uid(const char *dirname, uid_t uid);
|
|
|
5f7b57 |
+int fdump_dir_stat_for_uid(int dir_fd, uid_t uid);
|
|
|
5f7b57 |
|
|
|
5f7b57 |
/* creates not_reportable file in the problem directory and saves the
|
|
|
5f7b57 |
reason to it, which prevents libreport from reporting the problem
|
|
|
5f7b57 |
diff --git a/src/lib/dump_dir.c b/src/lib/dump_dir.c
|
|
|
5f7b57 |
index 16cd987..a0e96e4 100644
|
|
|
5f7b57 |
--- a/src/lib/dump_dir.c
|
|
|
5f7b57 |
+++ b/src/lib/dump_dir.c
|
|
|
5f7b57 |
@@ -417,12 +417,8 @@ static char* rm_trailing_slashes(const char *dir)
|
|
|
5f7b57 |
return xstrndup(dir, len);
|
|
|
5f7b57 |
}
|
|
|
5f7b57 |
|
|
|
5f7b57 |
-struct dump_dir *dd_opendir(const char *dir, int flags)
|
|
|
5f7b57 |
+static struct dump_dir *dd_do_open(struct dump_dir *dd, int flags)
|
|
|
5f7b57 |
{
|
|
|
5f7b57 |
- struct dump_dir *dd = dd_init();
|
|
|
5f7b57 |
-
|
|
|
5f7b57 |
- dir = dd->dd_dirname = rm_trailing_slashes(dir);
|
|
|
5f7b57 |
- dd->dd_fd = open(dir, O_DIRECTORY | O_NOFOLLOW);
|
|
|
5f7b57 |
struct stat stat_buf;
|
|
|
5f7b57 |
if (dd->dd_fd < 0)
|
|
|
5f7b57 |
goto cant_access;
|
|
|
5f7b57 |
@@ -440,6 +436,7 @@ struct dump_dir *dd_opendir(const char *dir, int flags)
|
|
|
5f7b57 |
/* Directory is not writable. If it seems to be readable,
|
|
|
5f7b57 |
* return "read only" dd, not NULL
|
|
|
5f7b57 |
*
|
|
|
5f7b57 |
+ *
|
|
|
5f7b57 |
* Does the directory have 'x' flag?
|
|
|
5f7b57 |
*/
|
|
|
5f7b57 |
if (faccessat(dd->dd_fd, ".", R_OK, AT_SYMLINK_NOFOLLOW) == 0)
|
|
|
5f7b57 |
@@ -461,7 +458,7 @@ struct dump_dir *dd_opendir(const char *dir, int flags)
|
|
|
5f7b57 |
* directory when run without arguments, because its option -d DIR
|
|
|
5f7b57 |
* defaults to "."!
|
|
|
5f7b57 |
*/
|
|
|
5f7b57 |
- error_msg("'%s' is not a problem directory", dir);
|
|
|
5f7b57 |
+ error_msg("'%s' is not a problem directory", dd->dd_dirname);
|
|
|
5f7b57 |
}
|
|
|
5f7b57 |
else
|
|
|
5f7b57 |
{
|
|
|
5f7b57 |
@@ -469,12 +466,12 @@ struct dump_dir *dd_opendir(const char *dir, int flags)
|
|
|
5f7b57 |
if (errno == ENOENT || errno == ENOTDIR)
|
|
|
5f7b57 |
{
|
|
|
5f7b57 |
if (!(flags & DD_FAIL_QUIETLY_ENOENT))
|
|
|
5f7b57 |
- error_msg("'%s' does not exist", dir);
|
|
|
5f7b57 |
+ error_msg("'%s' does not exist", dd->dd_dirname);
|
|
|
5f7b57 |
}
|
|
|
5f7b57 |
else
|
|
|
5f7b57 |
{
|
|
|
5f7b57 |
if (!(flags & DD_FAIL_QUIETLY_EACCES))
|
|
|
5f7b57 |
- perror_msg("Can't access '%s'", dir);
|
|
|
5f7b57 |
+ perror_msg("Can't access '%s'", dd->dd_dirname);
|
|
|
5f7b57 |
}
|
|
|
5f7b57 |
}
|
|
|
5f7b57 |
dd_close(dd);
|
|
|
5f7b57 |
@@ -488,7 +485,7 @@ struct dump_dir *dd_opendir(const char *dir, int flags)
|
|
|
5f7b57 |
/* In case caller would want to create more files, he'll need uid:gid */
|
|
|
5f7b57 |
if (fstat(dd->dd_fd, &stat_buf) != 0)
|
|
|
5f7b57 |
{
|
|
|
5f7b57 |
- error_msg("Can't stat '%s'", dir);
|
|
|
5f7b57 |
+ error_msg("Can't stat '%s'", dd->dd_dirname);
|
|
|
5f7b57 |
dd_close(dd);
|
|
|
5f7b57 |
return NULL;
|
|
|
5f7b57 |
}
|
|
|
5f7b57 |
@@ -499,6 +496,34 @@ struct dump_dir *dd_opendir(const char *dir, int flags)
|
|
|
5f7b57 |
return dd;
|
|
|
5f7b57 |
}
|
|
|
5f7b57 |
|
|
|
5f7b57 |
+int dd_openfd(const char *dir)
|
|
|
5f7b57 |
+{
|
|
|
5f7b57 |
+ return open(dir, O_DIRECTORY | O_NOFOLLOW);
|
|
|
5f7b57 |
+}
|
|
|
5f7b57 |
+
|
|
|
5f7b57 |
+struct dump_dir *dd_fdopendir(int dir_fd, const char *dir, int flags)
|
|
|
5f7b57 |
+{
|
|
|
5f7b57 |
+ struct dump_dir *dd = dd_init();
|
|
|
5f7b57 |
+
|
|
|
5f7b57 |
+ dd->dd_dirname = rm_trailing_slashes(dir);
|
|
|
5f7b57 |
+ dd->dd_fd = dir_fd;
|
|
|
5f7b57 |
+
|
|
|
5f7b57 |
+ /* Do not interpret errno in dd_do_open() if dd_fd < 0 */
|
|
|
5f7b57 |
+ errno = 0;
|
|
|
5f7b57 |
+ return dd_do_open(dd, flags);
|
|
|
5f7b57 |
+}
|
|
|
5f7b57 |
+
|
|
|
5f7b57 |
+struct dump_dir *dd_opendir(const char *dir, int flags)
|
|
|
5f7b57 |
+{
|
|
|
5f7b57 |
+ struct dump_dir *dd = dd_init();
|
|
|
5f7b57 |
+
|
|
|
5f7b57 |
+ dd->dd_dirname = rm_trailing_slashes(dir);
|
|
|
5f7b57 |
+ /* dd_do_open validates dd_fd */
|
|
|
5f7b57 |
+ dd->dd_fd = dd_openfd(dir);
|
|
|
5f7b57 |
+
|
|
|
5f7b57 |
+ return dd_do_open(dd, flags);
|
|
|
5f7b57 |
+}
|
|
|
5f7b57 |
+
|
|
|
5f7b57 |
/* Create a fresh empty debug dump dir which is owned bu the calling user. If
|
|
|
5f7b57 |
* you want to create the directory with meaningful ownership you should
|
|
|
5f7b57 |
* consider using dd_create() function or you can modify the ownership
|
|
|
5f7b57 |
@@ -936,7 +961,7 @@ int dd_chown(struct dump_dir *dd, uid_t new_uid)
|
|
|
5f7b57 |
error_msg_and_die("dump_dir is not opened"); /* bug */
|
|
|
5f7b57 |
|
|
|
5f7b57 |
struct stat statbuf;
|
|
|
5f7b57 |
- if (!(stat(dd->dd_dirname, &statbuf) == 0 && S_ISDIR(statbuf.st_mode)))
|
|
|
5f7b57 |
+ if (!fstat(dd->dd_fd, &statbuf) == 0)
|
|
|
5f7b57 |
{
|
|
|
5f7b57 |
perror_msg("stat('%s')", dd->dd_dirname);
|
|
|
5f7b57 |
return 1;
|
|
|
5f7b57 |
@@ -1352,12 +1377,12 @@ static bool uid_in_group(uid_t uid, gid_t gid)
|
|
|
5f7b57 |
}
|
|
|
5f7b57 |
#endif
|
|
|
5f7b57 |
|
|
|
5f7b57 |
-int dump_dir_stat_for_uid(const char *dirname, uid_t uid)
|
|
|
5f7b57 |
+int fdump_dir_stat_for_uid(int dir_fd, uid_t uid)
|
|
|
5f7b57 |
{
|
|
|
5f7b57 |
struct stat statbuf;
|
|
|
5f7b57 |
- if (stat(dirname, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode))
|
|
|
5f7b57 |
+ if (fstat(dir_fd, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode))
|
|
|
5f7b57 |
{
|
|
|
5f7b57 |
- log_debug("can't get stat of '%s': not a problem directory", dirname);
|
|
|
5f7b57 |
+ log_debug("can't get stat: not a problem directory");
|
|
|
5f7b57 |
errno = ENOTDIR;
|
|
|
5f7b57 |
return -1;
|
|
|
5f7b57 |
}
|
|
|
5f7b57 |
@@ -1367,7 +1392,7 @@ int dump_dir_stat_for_uid(const char *dirname, uid_t uid)
|
|
|
5f7b57 |
int ddstat = 0;
|
|
|
5f7b57 |
if (uid == 0 || (statbuf.st_mode & S_IROTH))
|
|
|
5f7b57 |
{
|
|
|
5f7b57 |
- log_debug("directory '%s' is accessible by %ld uid", dirname, (long)uid);
|
|
|
5f7b57 |
+ log_debug("directory is accessible by %ld uid", (long)uid);
|
|
|
5f7b57 |
ddstat |= DD_STAT_ACCESSIBLE_BY_UID;
|
|
|
5f7b57 |
}
|
|
|
5f7b57 |
|
|
|
5f7b57 |
@@ -1377,7 +1402,7 @@ int dump_dir_stat_for_uid(const char *dirname, uid_t uid)
|
|
|
5f7b57 |
if (uid_in_group(uid, statbuf.st_gid))
|
|
|
5f7b57 |
#endif
|
|
|
5f7b57 |
{
|
|
|
5f7b57 |
- log_debug("%ld uid owns directory '%s'", (long)uid, dirname);
|
|
|
5f7b57 |
+ log_debug("%ld uid owns directory", (long)uid);
|
|
|
5f7b57 |
ddstat |= DD_STAT_ACCESSIBLE_BY_UID;
|
|
|
5f7b57 |
ddstat |= DD_STAT_OWNED_BY_UID;
|
|
|
5f7b57 |
}
|
|
|
5f7b57 |
@@ -1385,6 +1410,33 @@ int dump_dir_stat_for_uid(const char *dirname, uid_t uid)
|
|
|
5f7b57 |
return ddstat;
|
|
|
5f7b57 |
}
|
|
|
5f7b57 |
|
|
|
5f7b57 |
+int dump_dir_stat_for_uid(const char *dirname, uid_t uid)
|
|
|
5f7b57 |
+{
|
|
|
5f7b57 |
+ int dir_fd = open(dirname, O_DIRECTORY | O_NOFOLLOW);
|
|
|
5f7b57 |
+ if (dir_fd < 0)
|
|
|
5f7b57 |
+ {
|
|
|
5f7b57 |
+ log_debug("can't open '%s': not a problem directory", dirname);
|
|
|
5f7b57 |
+ errno = ENOTDIR;
|
|
|
5f7b57 |
+ return -1;
|
|
|
5f7b57 |
+ }
|
|
|
5f7b57 |
+
|
|
|
5f7b57 |
+ int r = fdump_dir_stat_for_uid(dir_fd, uid);
|
|
|
5f7b57 |
+ close(dir_fd);
|
|
|
5f7b57 |
+ return r;
|
|
|
5f7b57 |
+}
|
|
|
5f7b57 |
+
|
|
|
5f7b57 |
+int fdump_dir_accessible_by_uid(int dir_fd, uid_t uid)
|
|
|
5f7b57 |
+{
|
|
|
5f7b57 |
+ int ddstat = fdump_dir_stat_for_uid(dir_fd, uid);
|
|
|
5f7b57 |
+
|
|
|
5f7b57 |
+ if (ddstat >= 0)
|
|
|
5f7b57 |
+ return ddstat & DD_STAT_ACCESSIBLE_BY_UID;
|
|
|
5f7b57 |
+
|
|
|
5f7b57 |
+ VERB3 pwarn_msg("can't determine accessibility for %ld uid", (long)uid);
|
|
|
5f7b57 |
+
|
|
|
5f7b57 |
+ return 0;
|
|
|
5f7b57 |
+}
|
|
|
5f7b57 |
+
|
|
|
5f7b57 |
int dump_dir_accessible_by_uid(const char *dirname, uid_t uid)
|
|
|
5f7b57 |
{
|
|
|
5f7b57 |
int ddstat = dump_dir_stat_for_uid(dirname, uid);
|
|
|
5f7b57 |
--
|
|
|
5f7b57 |
1.8.3.1
|
|
|
5f7b57 |
|