Chromium Code Reviews| Index: base/file_util_posix.cc |
| diff --git a/base/file_util_posix.cc b/base/file_util_posix.cc |
| index 9904584abd3c0fa9059c7ce1f276caaa9642b065..9b9be8861a3c8be5609283158daaf6f305fe393b 100644 |
| --- a/base/file_util_posix.cc |
| +++ b/base/file_util_posix.cc |
| @@ -8,6 +8,7 @@ |
| #include <errno.h> |
| #include <fcntl.h> |
| #include <fnmatch.h> |
| +#include <grp.h> |
| #include <libgen.h> |
| #include <limits.h> |
| #include <stdio.h> |
| @@ -73,12 +74,20 @@ static int CallStat(const char *path, stat_wrapper_t *sb) { |
| base::ThreadRestrictions::AssertIOAllowed(); |
| return stat(path, sb); |
| } |
| +static int CallLstat(const char *path, stat_wrapper_t *sb) { |
| + base::ThreadRestrictions::AssertIOAllowed(); |
| + return lstat(path, sb); |
| +} |
| #else |
| typedef struct stat64 stat_wrapper_t; |
| static int CallStat(const char *path, stat_wrapper_t *sb) { |
| base::ThreadRestrictions::AssertIOAllowed(); |
| return stat64(path, sb); |
| } |
| +static int CallLstat(const char *path, stat_wrapper_t *sb) { |
| + base::ThreadRestrictions::AssertIOAllowed(); |
| + return lstat64(path, sb); |
| +} |
| #endif |
| static std::string TempFileName() { |
| @@ -940,4 +949,103 @@ bool CopyFile(const FilePath& from_path, const FilePath& to_path) { |
| } |
| #endif // defined(OS_MACOSX) |
| +// Helper for IsPathControlledByUser. |
| +bool SpecificPathControlledByUser(const FilePath& path, |
| + uid_t owner_uid, |
| + gid_t group_gid) { |
| + stat_wrapper_t stat_info; |
| + if (CallLstat(path.value().c_str(), &stat_info) != 0) { |
| + PLOG(ERROR) << "Failed to get information on path " |
| + << path.value(); |
| + return false; |
| + } |
| + |
| + if (S_ISLNK(stat_info.st_mode)) { |
| + LOG(ERROR) << "Path " << path.value() |
| + << " is a symbolic link."; |
| + return false; |
| + } |
| + |
| + if (stat_info.st_uid != owner_uid) { |
| + LOG(ERROR) << "Path " << path.value() |
| + << " is owned by the wrong user."; |
| + return false; |
| + } |
| + |
| + if (stat_info.st_gid != group_gid) { |
| + LOG(ERROR) << "Path " << path.value() |
| + << " is owned by the wrong group."; |
| + return false; |
| + } |
| + |
| + if (stat_info.st_mode & S_IWOTH) { |
| + LOG(ERROR) << "Path "<< path.value() |
| + << " is writable by any user."; |
| + return false; |
| + } |
| + |
| + return true; |
| +} |
| + |
| +bool IsPathControlledByUser(const FilePath& base, |
| + const FilePath& path, |
| + uid_t owner_uid, |
| + gid_t group_gid) { |
| + std::vector<FilePath::StringType> base_components; |
| + std::vector<FilePath::StringType> path_components; |
| + |
| + base.GetComponents(&base_components); |
| + path.GetComponents(&path_components); |
| + |
| + std::vector<FilePath::StringType>::const_iterator ib, ip; |
| + ib = base_components.begin(); |
|
TVL
2011/08/26 20:17:15
this is also in the loop setup
Sam Kerner (Chrome)
2011/08/29 14:19:48
Done.
|
| + for (ib = base_components.begin(), ip = path_components.begin(); |
| + ib != base_components.end(); ++ib, ++ip) { |
| + // |base| must be a subpath of |path|, so all components of base |
| + // should match path. |
| + if (ip == path_components.end()) { |
| + LOG(ERROR) << "|path| can't be a subdirectory of |base|. base = " |
| + << base.value() << " path = " << path.value(); |
| + return false; |
| + } |
| + if (*ip != *ib) { |
| + LOG(ERROR) << "|base| must be a subdirectory of |path|. base = " |
| + << base.value() << " path = " << path.value(); |
| + return false; |
| + } |
| + } |
|
TVL
2011/08/26 20:17:15
FilePath has IsParent()
Sam Kerner (Chrome)
2011/08/29 14:19:48
Used it to make an early-out at the top of the fun
|
| + |
| + FilePath current_path = base; |
| + if (!SpecificPathControlledByUser(current_path, owner_uid, group_gid)) |
| + return false; |
| + |
| + for (; ip != path_components.end(); ++ip) { |
| + current_path = current_path.Append(*ip); |
| + if (!SpecificPathControlledByUser(current_path, owner_uid, group_gid)) |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| +bool IsPathControlledByAdmin(const FilePath& path) { |
| + const unsigned kRootUid = 0; |
| + const FilePath kFileSystemRoot("/"); |
| + |
| + // The name of the administrator group on mac os. |
| + const char kAdminGroupName[] = "admin"; |
| + |
| + // Reading the groups database may touch the file system. |
| + base::ThreadRestrictions::AssertIOAllowed(); |
| + |
| + struct group *groupRecord = getgrnam(kAdminGroupName); |
| + if (!groupRecord) { |
| + PLOG(ERROR) << "Could not get the group ID of group \"" |
| + << kAdminGroupName << "\"."; |
| + return false; |
| + } |
| + |
| + return IsPathControlledByUser( |
| + kFileSystemRoot, path, kRootUid, groupRecord->gr_gid); |
| +} |
| + |
| } // namespace file_util |