Linux: Read lower-precedence policies from $SNAP_DATA/policies in snaps

Canonical's Chromium snap carries snap-policies-path.patch, which
replaces chrome::DIR_POLICY_FILES with $SNAP_DATA/policies when
SNAP_DATA is set. That can't be upstreamed as is: SNAP_DATA is an
environment variable, so on a regular deb or rpm install any user could
bypass enterprise policy (and the enrollment token, which is also read
from DIR_POLICY_FILES) with `SNAP_DATA=/tmp/x google-chrome`.

The standard policy directory already works in a snap through the
browser-support interface. $SNAP_DATA/policies is still useful where
/etc/opt isn't writable (Ubuntu Core) and for per-snap policies, so add
it as an additional, lower-precedence source of platform policy:

- ConfigDirPolicyLoader takes a vector<ConfigDirectory> in decreasing
  order of precedence, each with the standard managed/ and recommended/
  layout and an optional OwnershipRequirement. Earlier directories win
  conflicts at the same level. This replaces the single-directory
  constructor; the only other caller (remoting's PolicyWatcher) is
  updated.
- When an OwnershipRequirement is set on a ConfigDirectory, a policy
  file in it is only read if the file and every directory from
  trusted_base_dir down to it are owned by trusted_uid, aren't symlinks
  and aren't writable by group or others. Symlinks in the configured
  path are resolved first, since $SNAP_DATA is /var/snap/<name>/current,
  a symlink to the current revision. This ownership check, not the
  environment, is the security boundary: an unprivileged user can't
  create a root-controlled directory under `/`. The check is only
  available on Linux and Mac, so OwnershipRequirement must not be used
  elsewhere.
- ChromeBrowserPolicyConnector appends $SNAP_DATA/policies (requiring
  root ownership up to `/`) when GetAppSandbox() is kSnap.
- base::VerifyPathControlledByUser() was Mac-only; enable it on Linux
  too for the ownership check.

chrome::DIR_POLICY_FILES and the enrollment token location are
unchanged.

Bug: 579508504
Change-Id: I3d27066da0cd30570ef03115b9f4b8b24ce59d08
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8706559
Reviewed-by: Lei Zhang <thestig@chromium.org>
Reviewed-by: Lambros Lambrou <lambroslambrou@chromium.org>
Reviewed-by: Nicolas Ouellet-Payeur <nicolaso@chromium.org>
Commit-Queue: Thomas Anderson <thomasanderson@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1740028}
NOKEYCHECK=True
GitOrigin-RevId: d0cf1233880531050b5636955fdf936426b94efe
3 files changed
tree: 8cc4024215c4a3484701c58e89289cda92da16dd
  1. allocator/
  2. android/
  3. apple/
  4. containers/
  5. debug/
  6. files/
  7. fuchsia/
  8. functional/
  9. hash/
  10. i18n/
  11. ios/
  12. json/
  13. logging/
  14. mac/
  15. macros/
  16. memory/
  17. memory_coordinator/
  18. message_loop/
  19. metrics/
  20. nix/
  21. numerics/
  22. posix/
  23. power_monitor/
  24. process/
  25. profiler/
  26. sampling_heap_profiler/
  27. strings/
  28. substring_set_matcher/
  29. synchronization/
  30. system/
  31. task/
  32. test/
  33. third_party/
  34. threading/
  35. time/
  36. timer/
  37. tools/
  38. trace_event/
  39. tracing/
  40. types/
  41. version_info/
  42. win/
  43. .ruff.toml
  44. at_exit.cc
  45. at_exit.h
  46. at_exit_unittest.cc
  47. atomic_ref_count.h
  48. atomic_sequence_num.h
  49. atomicops.cc
  50. atomicops.h
  51. atomicops_internals_portable.h
  52. atomicops_unittest.cc
  53. auto_reset.h
  54. auto_reset_unittest.cc
  55. barrier_callback.h
  56. barrier_callback_unittest.cc
  57. barrier_closure.cc
  58. barrier_closure.h
  59. barrier_closure_unittest.cc
  60. base64.cc
  61. base64.h
  62. base64_decode_fuzzer.cc
  63. base64_encode_fuzzer.cc
  64. base64_perftest.cc
  65. base64_rust_shim.rs
  66. base64_unittest.cc
  67. base64url.cc
  68. base64url.h
  69. base64url_unittest.cc
  70. base_export.h
  71. base_paths.cc
  72. base_paths.h
  73. base_paths_android.cc
  74. base_paths_android.h
  75. base_paths_apple.cc
  76. base_paths_apple.h
  77. base_paths_fuchsia.cc
  78. base_paths_ios.h
  79. base_paths_ios.mm
  80. base_paths_mac.h
  81. base_paths_mac.mm
  82. base_paths_posix.cc
  83. base_paths_posix.h
  84. base_paths_win.cc
  85. base_paths_win.h
  86. base_rust_unittests.rs
  87. base_switches.h
  88. bit_cast.h
  89. bit_cast_unittest.cc
  90. bits.h
  91. bits_unittest.cc
  92. BUILD.gn
  93. build_time.h
  94. build_time_unittest.cc
  95. byte_conversions_perftest.cc
  96. byte_size.cc
  97. byte_size.h
  98. byte_size_nocompile.nc
  99. byte_size_unittest.cc
  100. callback_list.cc
  101. callback_list.h
  102. callback_list_nocompile.nc
  103. callback_list_unittest.cc
  104. cancelable_callback.h
  105. cancelable_callback_unittest.cc
  106. check.cc
  107. check.h
  108. check_deref.h
  109. check_deref_nocompile.nc
  110. check_deref_unittest.cc
  111. check_example.cc
  112. check_is_test.cc
  113. check_is_test.h
  114. check_is_test_unittest.cc
  115. check_nocompile.nc
  116. check_op.cc
  117. check_op.h
  118. check_unittest.cc
  119. check_version_internal.h.in
  120. command_line.cc
  121. command_line.h
  122. command_line.rs
  123. command_line_fuzzer.cc
  124. command_line_rust_shim.cc
  125. command_line_rust_shim.h
  126. command_line_unittest.cc
  127. command_line_unittest.rs
  128. compiler_hardening_test.cc
  129. compiler_specific.h
  130. component_export.h
  131. component_export_unittest.cc
  132. cpu.cc
  133. cpu.h
  134. cpu_unittest.cc
  135. critical_closure.h
  136. critical_closure_internal_ios.mm
  137. dcheck_is_on.h
  138. DEPS
  139. DIR_METADATA
  140. enterprise_util.cc
  141. enterprise_util.h
  142. enterprise_util_mac.mm
  143. enterprise_util_mac_unittest.mm
  144. enterprise_util_win.cc
  145. environment.cc
  146. environment.h
  147. environment_unittest.cc
  148. export_template.h
  149. feature.h
  150. feature.rs
  151. feature_internal.h
  152. feature_list.cc
  153. feature_list.h
  154. feature_list_internal.cc
  155. feature_list_internal.h
  156. feature_list_unittest.cc
  157. feature_nocompile.nc
  158. feature_rust_shim.cc
  159. feature_rust_shim.h
  160. feature_unittest.rs
  161. feature_visitor.h
  162. features.cc
  163. features.h
  164. file_descriptor_posix.cc
  165. file_descriptor_posix.h
  166. file_descriptor_store.cc
  167. file_descriptor_store.h
  168. file_version_info.h
  169. file_version_info_apple.h
  170. file_version_info_apple.mm
  171. file_version_info_win.cc
  172. file_version_info_win.h
  173. file_version_info_win_unittest.cc
  174. format_macros.h
  175. gmock_unittest.cc
  176. gtest_prod_util.h
  177. immediate_crash.h
  178. immediate_crash_unittest.cc
  179. lazy_instance.h
  180. lazy_instance_helpers.cc
  181. lazy_instance_helpers.h
  182. lazy_instance_unittest.cc
  183. libcpp_hardening_test.cc
  184. linux_util.cc
  185. linux_util.h
  186. linux_util_unittest.cc
  187. location.cc
  188. location.h
  189. location_unittest.cc
  190. logging.cc
  191. logging.h
  192. logging_chromeos.cc
  193. logging_nocompile.nc
  194. logging_unittest.cc
  195. logging_win.cc
  196. logging_win.h
  197. moving_window.h
  198. moving_window_unittest.cc
  199. native_library.h
  200. native_library_apple.mm
  201. native_library_fuchsia.cc
  202. native_library_posix.cc
  203. native_library_unittest.cc
  204. native_library_win.cc
  205. no_destructor.h
  206. no_destructor_nocompile.nc
  207. no_destructor_unittest.cc
  208. not_fatal_until.h
  209. not_fatal_until_severity_override.cc
  210. not_fatal_until_severity_override.h
  211. notimplemented.h
  212. notreached.h
  213. observer_list.h
  214. observer_list_internal.cc
  215. observer_list_internal.h
  216. observer_list_nocompile.nc
  217. observer_list_perftest.cc
  218. observer_list_threadsafe.cc
  219. observer_list_threadsafe.h
  220. observer_list_threadsafe_unittest.cc
  221. observer_list_types.cc
  222. observer_list_types.h
  223. observer_list_unittest.cc
  224. one_shot_event.cc
  225. one_shot_event.h
  226. one_shot_event_unittest.cc
  227. os_compat_android.cc
  228. os_compat_android.h
  229. OWNERS
  230. parameter_pack.h
  231. parameter_pack_unittest.cc
  232. path_service.cc
  233. path_service.h
  234. path_service_unittest.cc
  235. pending_task.cc
  236. pending_task.h
  237. pickle.cc
  238. pickle.h
  239. pickle_fuzzer.cc
  240. pickle_unittest.cc
  241. PRESUBMIT.py
  242. protobuf_hardening_test.cc
  243. protobuf_hardening_test_support.proto
  244. rand_util.cc
  245. rand_util.h
  246. rand_util_fuchsia.cc
  247. rand_util_perftest.cc
  248. rand_util_posix.cc
  249. rand_util_unittest.cc
  250. rand_util_win.cc
  251. README.md
  252. run_loop.cc
  253. run_loop.h
  254. run_loop.rs
  255. run_loop_nocompile.nc
  256. run_loop_rust_shim.cc
  257. run_loop_rust_shim.h
  258. run_loop_unittest.cc
  259. safe_numerics_nocompile.nc
  260. safe_numerics_unittest.cc
  261. scoped_add_feature_flags.cc
  262. scoped_add_feature_flags.h
  263. scoped_add_feature_flags_unittest.cc
  264. scoped_clear_last_error.h
  265. scoped_clear_last_error_unittest.cc
  266. scoped_clear_last_error_win.cc
  267. scoped_environment_variable_override.cc
  268. scoped_environment_variable_override.h
  269. scoped_generic.h
  270. scoped_generic_unittest.cc
  271. scoped_multi_source_observation.h
  272. scoped_multi_source_observation_unittest.cc
  273. scoped_native_library.cc
  274. scoped_native_library.h
  275. scoped_native_library_unittest.cc
  276. scoped_observation.h
  277. scoped_observation_traits.h
  278. scoped_observation_unittest.cc
  279. SECURITY_OWNERS
  280. security_unittest.cc
  281. sequence_checker.cc
  282. sequence_checker.h
  283. sequence_checker_impl.cc
  284. sequence_checker_impl.h
  285. sequence_checker_nocompile.nc
  286. sequence_checker_unittest.cc
  287. sequence_token.cc
  288. sequence_token.h
  289. sequence_token_unittest.cc
  290. stack_canary_linux.cc
  291. stack_canary_linux.h
  292. stack_canary_linux_unittest.cc
  293. state_transitions.h
  294. state_transitions_unittest.cc
  295. std_clamp_unittest.cc
  296. stl_util.h
  297. stl_util_unittest.cc
  298. supports_user_data.cc
  299. supports_user_data.h
  300. supports_user_data_unittest.cc
  301. sync_socket.cc
  302. sync_socket.h
  303. sync_socket_posix.cc
  304. sync_socket_unittest.cc
  305. sync_socket_win.cc
  306. sys_byteorder.h
  307. sys_byteorder_unittest.cc
  308. syslog_logging.cc
  309. syslog_logging.h
  310. thread_annotations.h
  311. thread_annotations_nocompile.nc
  312. thread_annotations_unittest.cc
  313. token.cc
  314. token.h
  315. token_unittest.cc
  316. tools_sanity_unittest.cc
  317. traits_bag.h
  318. traits_bag_nocompile.nc
  319. traits_bag_unittest.cc
  320. tuple.h
  321. tuple_unittest.cc
  322. unguessable_token.cc
  323. unguessable_token.h
  324. unguessable_token.rs
  325. unguessable_token_unittest.cc
  326. unsafe_buffers_nocompile.nc
  327. unsafe_buffers_unittest.cc
  328. uuid.cc
  329. uuid.h
  330. uuid_unittest.cc
  331. value_iterators.cc
  332. value_iterators.h
  333. value_iterators_unittest.cc
  334. values.cc
  335. values.h
  336. values_nocompile.nc
  337. values_unittest.cc
  338. version.cc
  339. version.h
  340. version_unittest.cc
  341. vlog.cc
  342. vlog.h
  343. vlog_unittest.cc
  344. write_build_date_header.py
README.md

What is this

Contains a written down set of principles and other information on //base. Please add to it!

About //base:

Chromium is a very mature project. Most things that are generally useful are already here and things not here aren't generally useful.

The bar for adding stuff to base is that it must have demonstrated wide applicability. Prefer to add things closer to where they're used (i.e. “not base”), and pull into base only when needed. In a project our size, sometimes even duplication is OK and inevitable.

Adding a new logging macro DPVELOG_NE is not more clear than just writing the stuff you want to log in a regular logging statement, even if it makes your calling code longer. Just add it to your own code.

If the code in question does not need to be used inside base, but will have multiple consumers across the codebase, consider placing it in a new directory under components/ instead.

base is written for the Chromium project and is not intended to be used outside it. Using base outside of src.git is explicitly not supported, and base makes no guarantees about API (or even ABI) stability (like all other code in Chromium). New code that depends on base/ must be in src.git. Code that's not in src.git but pulled in through DEPS (for example, v8) cannot use base.

Qualifications for being in //base OWNERS

  • interest and ability to learn low level/high detail/complex c++ stuff
  • inclination to always ask why and understand everything (including external interactions like win32) rather than just hoping the author did it right
  • mentorship/experience
  • demonstrated good judgement (esp with regards to public APIs) over a length of time

Owners are added when a contributor has shown the above qualifications and when they express interest. There isn't an upper bound on the number of OWNERS.

Design and naming

  • Be sure to use the base namespace.
  • STL-like constructs should adhere as closely to STL as possible. Functions and behaviors not present in STL should only be added when they are related to the specific data structure implemented by the container.
  • For STL-like constructs our policy is that they should use STL-like naming even when it may conflict with the style guide. So functions and class names should be lower case with underscores. Non-STL-like classes and functions should use Google naming.

Performance testing

Since the primitives provided by //base are used very widely, it is important to ensure they scale to the necessary workloads and perform well under all supported platforms. The base_perftests target is a suite of synthetic microbenchmarks that measure performance in various scenarios:

  • BasicPostTaskPerfTest: Exercises MessageLoopTaskRunner's multi-threaded queue in isolation.
  • ConditionVariablePerfTest: Measures thread switching cost of condition variables.
  • IntegratedPostTaskPerfTest: Exercises the full MessageLoop/RunLoop machinery.
  • JSONPerfTest: Tests JSONWriter and JSONReader performance.
  • MessageLoopPerfTest: Measures the speed of task posting in various configurations.
  • ObserverListPerfTest: Exercises adding, removing and signalling observers.
  • PartitionLockPerfTest: Tests the implementation of Lock used in PartitionAlloc
  • PthreadEventPerfTest: Establishes the baseline thread switching cost using pthreads.
  • RandUtilPerfTest: Measures the time it takes to generate random numbers.
  • ScheduleWorkTest: Measures the overhead of MessagePump::ScheduleWork.
  • SequenceManagerPerfTest: Benchmarks SequenceManager scheduling with various underlying task runners.
  • TaskObserverPerfTest: Measures the incremental cost of adding task observers.
  • TaskPerfTest: Checks the cost of posting tasks between threads.
  • ThreadLocalStoragePerfTest: Exercises different mechanisms for accessing data associated with the current thread (C++ thread_local, the implementation in //base, the POSIX/WinAPI directly)
  • WaitableEvent{Thread,}PerfTest: Measures waitable events in single and multithreaded scenarios.

Regressions in these benchmarks can generally by caused by 1) operating system changes, 2) compiler version or flag changes or 3) changes in //base code itself.

Rust code

Rust code in base should be organized into very small crates, split up by function. Merging crates is sometimes unavoidable (due to dependency cycles or the orphaning rule).

Rust files should live near the equivalent C++ files (if any), and use the same naming scheme (for example, run_loop.rs, not run_loop_rust.rs).

When adding FFI shims, prefer separate _shim.h files rather than adding code to existing C++ files. This helps avoid circular dependencies with the //base target. It also avoids adding code to commonly-used headers, which can increase compile size by a lot.

Crates which you expect to be widely used should be added to the public_deps of the //base:base_rust target, so that developers can simply depend on //base:base_rust the same way they do with //base.