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..15a26de0e4b7276e194ddb17471f7d4aff9e2fc1 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,101 @@ bool CopyFile(const FilePath& from_path, const FilePath& to_path) { |
| } |
| #endif // defined(OS_MACOSX) |
| +// Helper for IsPathControlledByUser. |
| +bool SpecificPathControlledByUser(const FilePath& path, |
|
Evan Martin
2011/08/30 17:26:38
Should be in the anon namespace if it's not part o
Sam Kerner (Chrome)
2011/09/16 18:12:59
Done.
|
| + 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."; |
|
Evan Martin
2011/08/30 17:26:38
It seems you're reporting it as an error when thes
Sam Kerner (Chrome)
2011/09/16 18:12:59
Done.
|
| + 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() |
|
Evan Martin
2011/08/30 17:26:38
space before <<
Sam Kerner (Chrome)
2011/09/16 18:12:59
Done.
|
| + << " 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) { |
| + if (base != path && !base.IsParent(path)) { |
| + LOG(ERROR) << "|base| must be a subdirectory of |path|. base = " |
| + << base.value() << " path = " << path.value(); |
| + return false; |
| + } |
| + |
| + 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; |
| + 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 should match. |
| + // If these CHECKs fail, look at the test that base is a parent of |
| + // path at the top of this function. |
| + CHECK(ip != path_components.end()); |
| + CHECK(*ip == *ib); |
| + } |
| + |
| + 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(); |
|
Evan Martin
2011/08/30 17:26:38
Thanks for doing this!
|
| + |
| + struct group *groupRecord = getgrnam(kAdminGroupName); |
|
Evan Martin
2011/08/30 17:26:38
group_record, not groupRecord
Sam Kerner (Chrome)
2011/09/16 18:12:59
Done.
|
| + 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 |