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

Side by Side Diff: base/file_util_posix.cc

Issue 12223014: Add path traversal protection to Move and CopyFile too. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 10 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_mac.mm ('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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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>
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 } 242 }
243 243
244 while (success && !directories.empty()) { 244 while (success && !directories.empty()) {
245 FilePath dir = FilePath(directories.top()); 245 FilePath dir = FilePath(directories.top());
246 directories.pop(); 246 directories.pop();
247 success = (rmdir(dir.value().c_str()) == 0); 247 success = (rmdir(dir.value().c_str()) == 0);
248 } 248 }
249 return success; 249 return success;
250 } 250 }
251 251
252 bool Move(const FilePath& from_path, const FilePath& to_path) { 252 bool MoveUnsafe(const FilePath& from_path, const FilePath& to_path) {
253 base::ThreadRestrictions::AssertIOAllowed(); 253 base::ThreadRestrictions::AssertIOAllowed();
254 // Windows compatibility: if to_path exists, from_path and to_path 254 // Windows compatibility: if to_path exists, from_path and to_path
255 // must be the same type, either both files, or both directories. 255 // must be the same type, either both files, or both directories.
256 stat_wrapper_t to_file_info; 256 stat_wrapper_t to_file_info;
257 if (CallStat(to_path.value().c_str(), &to_file_info) == 0) { 257 if (CallStat(to_path.value().c_str(), &to_file_info) == 0) {
258 stat_wrapper_t from_file_info; 258 stat_wrapper_t from_file_info;
259 if (CallStat(from_path.value().c_str(), &from_file_info) == 0) { 259 if (CallStat(from_path.value().c_str(), &from_file_info) == 0) {
260 if (S_ISDIR(to_file_info.st_mode) != S_ISDIR(from_file_info.st_mode)) 260 if (S_ISDIR(to_file_info.st_mode) != S_ISDIR(from_file_info.st_mode))
261 return false; 261 return false;
262 } else { 262 } else {
(...skipping 742 matching lines...) Expand 10 before | Expand all | Expand 10 after
1005 #endif 1005 #endif
1006 1006
1007 FilePath rv; 1007 FilePath rv;
1008 if (file_util::GetTempDir(&rv)) 1008 if (file_util::GetTempDir(&rv))
1009 return rv; 1009 return rv;
1010 1010
1011 // Last resort. 1011 // Last resort.
1012 return FilePath("/tmp"); 1012 return FilePath("/tmp");
1013 } 1013 }
1014 1014
1015 bool CopyFile(const FilePath& from_path, const FilePath& to_path) { 1015 bool CopyFileUnsafe(const FilePath& from_path, const FilePath& to_path) {
1016 base::ThreadRestrictions::AssertIOAllowed(); 1016 base::ThreadRestrictions::AssertIOAllowed();
1017 int infile = HANDLE_EINTR(open(from_path.value().c_str(), O_RDONLY)); 1017 int infile = HANDLE_EINTR(open(from_path.value().c_str(), O_RDONLY));
1018 if (infile < 0) 1018 if (infile < 0)
1019 return false; 1019 return false;
1020 1020
1021 int outfile = HANDLE_EINTR(creat(to_path.value().c_str(), 0666)); 1021 int outfile = HANDLE_EINTR(creat(to_path.value().c_str(), 0666));
1022 if (outfile < 0) { 1022 if (outfile < 0) {
1023 ignore_result(HANDLE_EINTR(close(infile))); 1023 ignore_result(HANDLE_EINTR(close(infile)));
1024 return false; 1024 return false;
1025 } 1025 }
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
1124 1124
1125 allowed_group_ids.insert(group_record->gr_gid); 1125 allowed_group_ids.insert(group_record->gr_gid);
1126 } 1126 }
1127 1127
1128 return VerifyPathControlledByUser( 1128 return VerifyPathControlledByUser(
1129 kFileSystemRoot, path, kRootUid, allowed_group_ids); 1129 kFileSystemRoot, path, kRootUid, allowed_group_ids);
1130 } 1130 }
1131 #endif // defined(OS_MACOSX) && !defined(OS_IOS) 1131 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
1132 1132
1133 } // namespace file_util 1133 } // namespace file_util
OLDNEW
« no previous file with comments | « base/file_util_mac.mm ('k') | base/file_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698