Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(120)

Side by Side Diff: base/file_util_posix.cc

Issue 7718021: Add external extensions json source in proper mac location. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Polish Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/file_util.h ('k') | base/file_util_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/file_util.h" 5 #include "base/file_util.h"
6 6
7 #include <dirent.h> 7 #include <dirent.h>
8 #include <errno.h> 8 #include <errno.h>
9 #include <fcntl.h> 9 #include <fcntl.h>
10 #include <fnmatch.h> 10 #include <fnmatch.h>
11 #include <grp.h>
11 #include <libgen.h> 12 #include <libgen.h>
12 #include <limits.h> 13 #include <limits.h>
13 #include <stdio.h> 14 #include <stdio.h>
14 #include <stdlib.h> 15 #include <stdlib.h>
15 #include <string.h> 16 #include <string.h>
16 #include <sys/errno.h> 17 #include <sys/errno.h>
17 #include <sys/mman.h> 18 #include <sys/mman.h>
18 #include <sys/param.h> 19 #include <sys/param.h>
19 #include <sys/stat.h> 20 #include <sys/stat.h>
20 #include <sys/time.h> 21 #include <sys/time.h>
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 } // namespace 67 } // namespace
67 68
68 #if defined(OS_OPENBSD) || defined(OS_FREEBSD) || \ 69 #if defined(OS_OPENBSD) || defined(OS_FREEBSD) || \
69 (defined(OS_MACOSX) && \ 70 (defined(OS_MACOSX) && \
70 MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5) 71 MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5)
71 typedef struct stat stat_wrapper_t; 72 typedef struct stat stat_wrapper_t;
72 static int CallStat(const char *path, stat_wrapper_t *sb) { 73 static int CallStat(const char *path, stat_wrapper_t *sb) {
73 base::ThreadRestrictions::AssertIOAllowed(); 74 base::ThreadRestrictions::AssertIOAllowed();
74 return stat(path, sb); 75 return stat(path, sb);
75 } 76 }
77 static int CallLstat(const char *path, stat_wrapper_t *sb) {
78 base::ThreadRestrictions::AssertIOAllowed();
79 return lstat(path, sb);
80 }
76 #else 81 #else
77 typedef struct stat64 stat_wrapper_t; 82 typedef struct stat64 stat_wrapper_t;
78 static int CallStat(const char *path, stat_wrapper_t *sb) { 83 static int CallStat(const char *path, stat_wrapper_t *sb) {
79 base::ThreadRestrictions::AssertIOAllowed(); 84 base::ThreadRestrictions::AssertIOAllowed();
80 return stat64(path, sb); 85 return stat64(path, sb);
81 } 86 }
87 static int CallLstat(const char *path, stat_wrapper_t *sb) {
88 base::ThreadRestrictions::AssertIOAllowed();
89 return lstat64(path, sb);
90 }
82 #endif 91 #endif
83 92
84 static std::string TempFileName() { 93 static std::string TempFileName() {
85 #if defined(OS_MACOSX) 94 #if defined(OS_MACOSX)
86 return StringPrintf(".%s.XXXXXX", base::mac::BaseBundleID()); 95 return StringPrintf(".%s.XXXXXX", base::mac::BaseBundleID());
87 #endif 96 #endif
88 97
89 #if defined(GOOGLE_CHROME_BUILD) 98 #if defined(GOOGLE_CHROME_BUILD)
90 return std::string(".com.google.Chrome.XXXXXX"); 99 return std::string(".com.google.Chrome.XXXXXX");
91 #else 100 #else
(...skipping 841 matching lines...) Expand 10 before | Expand all | Expand 10 after
933 942
934 if (HANDLE_EINTR(close(infile)) < 0) 943 if (HANDLE_EINTR(close(infile)) < 0)
935 result = false; 944 result = false;
936 if (HANDLE_EINTR(close(outfile)) < 0) 945 if (HANDLE_EINTR(close(outfile)) < 0)
937 result = false; 946 result = false;
938 947
939 return result; 948 return result;
940 } 949 }
941 #endif // defined(OS_MACOSX) 950 #endif // defined(OS_MACOSX)
942 951
952 // Helper for IsPathControlledByUser.
953 bool SpecificPathControlledByUser(const FilePath& path,
954 uid_t owner_uid,
955 gid_t group_gid) {
956 stat_wrapper_t stat_info;
957 if (CallLstat(path.value().c_str(), &stat_info) != 0) {
958 PLOG(ERROR) << "Failed to get information on path "
959 << path.value();
960 return false;
961 }
962
963 if (S_ISLNK(stat_info.st_mode)) {
964 LOG(ERROR) << "Path " << path.value()
965 << " is a symbolic link.";
966 return false;
967 }
968
969 if (stat_info.st_uid != owner_uid) {
970 LOG(ERROR) << "Path " << path.value()
971 << " is owned by the wrong user.";
972 return false;
973 }
974
975 if (stat_info.st_gid != group_gid) {
976 LOG(ERROR) << "Path " << path.value()
977 << " is owned by the wrong group.";
978 return false;
979 }
980
981 if (stat_info.st_mode & S_IWOTH) {
982 LOG(ERROR) << "Path "<< path.value()
983 << " is writable by any user.";
984 return false;
985 }
986
987 return true;
988 }
989
990 bool IsPathControlledByUser(const FilePath& base,
991 const FilePath& path,
992 uid_t owner_uid,
993 gid_t group_gid) {
994 std::vector<FilePath::StringType> base_components;
995 std::vector<FilePath::StringType> path_components;
996
997 base.GetComponents(&base_components);
998 path.GetComponents(&path_components);
999
1000 std::vector<FilePath::StringType>::const_iterator ib, ip;
1001 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.
1002 for (ib = base_components.begin(), ip = path_components.begin();
1003 ib != base_components.end(); ++ib, ++ip) {
1004 // |base| must be a subpath of |path|, so all components of base
1005 // should match path.
1006 if (ip == path_components.end()) {
1007 LOG(ERROR) << "|path| can't be a subdirectory of |base|. base = "
1008 << base.value() << " path = " << path.value();
1009 return false;
1010 }
1011 if (*ip != *ib) {
1012 LOG(ERROR) << "|base| must be a subdirectory of |path|. base = "
1013 << base.value() << " path = " << path.value();
1014 return false;
1015 }
1016 }
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
1017
1018 FilePath current_path = base;
1019 if (!SpecificPathControlledByUser(current_path, owner_uid, group_gid))
1020 return false;
1021
1022 for (; ip != path_components.end(); ++ip) {
1023 current_path = current_path.Append(*ip);
1024 if (!SpecificPathControlledByUser(current_path, owner_uid, group_gid))
1025 return false;
1026 }
1027 return true;
1028 }
1029
1030 bool IsPathControlledByAdmin(const FilePath& path) {
1031 const unsigned kRootUid = 0;
1032 const FilePath kFileSystemRoot("/");
1033
1034 // The name of the administrator group on mac os.
1035 const char kAdminGroupName[] = "admin";
1036
1037 // Reading the groups database may touch the file system.
1038 base::ThreadRestrictions::AssertIOAllowed();
1039
1040 struct group *groupRecord = getgrnam(kAdminGroupName);
1041 if (!groupRecord) {
1042 PLOG(ERROR) << "Could not get the group ID of group \""
1043 << kAdminGroupName << "\".";
1044 return false;
1045 }
1046
1047 return IsPathControlledByUser(
1048 kFileSystemRoot, path, kRootUid, groupRecord->gr_gid);
1049 }
1050
943 } // namespace file_util 1051 } // namespace file_util
OLDNEW
« no previous file with comments | « base/file_util.h ('k') | base/file_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698