Skip to content

Commit

Permalink
Bug 1866606 - Support TaggedAnonymousMemory on all Linux r=jld
Browse files Browse the repository at this point in the history
Change ANDROID ifdefs to XP_LINUX and allow PR_SET_VMA in the sandbox.

Differential Revision: https://phabricator.services.mozilla.com/D195064
  • Loading branch information
benjaminp committed Dec 23, 2023
1 parent 7e131cc commit 54b88de
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
4 changes: 2 additions & 2 deletions mfbt/TaggedAnonymousMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifdef ANDROID
#ifdef XP_LINUX

# include "mozilla/TaggedAnonymousMemory.h"

Expand Down Expand Up @@ -90,4 +90,4 @@ void* MozTaggedAnonymousMmap(void* aAddr, size_t aLength, int aProt, int aFlags,
return mapped;
}

#endif // ANDROID
#endif // XP_LINUX
23 changes: 11 additions & 12 deletions mfbt/TaggedAnonymousMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// Some Linux kernels -- specifically, newer versions of Android and
// some B2G devices -- have a feature for assigning names to ranges of
// anonymous memory (i.e., memory that doesn't have a "name" in the
// form of an underlying mapped file). These names are reported in
// /proc/<pid>/smaps alongside system-level memory usage information
// such as Proportional Set Size (memory usage adjusted for sharing
// between processes), which allows reporting this information at a
// finer granularity than would otherwise be possible (e.g.,
// separating malloc() heap from JS heap).
// Linux kernels since 5.17 have a feature for assigning names to
// ranges of anonymous memory (i.e., memory that doesn't have a "name"
// in the form of an underlying mapped file). These names are
// reported in /proc/<pid>/smaps alongside system-level memory usage
// information such as Proportional Set Size (memory usage adjusted
// for sharing between processes), which allows reporting this
// information at a finer granularity than would otherwise be possible
// (e.g., separating malloc() heap from JS heap).
//
// Existing memory can be tagged with MozTagAnonymousMemory(); it will
// tag the range of complete pages containing the given interval, so
Expand Down Expand Up @@ -43,7 +42,7 @@

# include "mozilla/Types.h"

# ifdef ANDROID
# ifdef XP_LINUX

# ifdef __cplusplus
extern "C" {
Expand All @@ -62,7 +61,7 @@ MFBT_API int MozTaggedMemoryIsSupported(void);
} // extern "C"
# endif

# else // ANDROID
# else // XP_LINUX

static inline void MozTagAnonymousMemory(const void* aPtr, size_t aLength,
const char* aTag) {}
Expand All @@ -80,7 +79,7 @@ static inline void* MozTaggedAnonymousMmap(void* aAddr, size_t aLength,

static inline int MozTaggedMemoryIsSupported(void) { return 0; }

# endif // ANDROID
# endif // XP_LINUX

#endif // !XP_WIN

Expand Down
20 changes: 17 additions & 3 deletions security/sandbox/linux/SandboxFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ using namespace sandbox::bpf_dsl;
# define PR_SET_PTRACER 0x59616d61
#endif

// Linux 5.17+
#ifndef PR_SET_VMA
# define PR_SET_VMA 0x53564d41
#endif
#ifndef PR_SET_VMA_ANON_NAME
# define PR_SET_VMA_ANON_NAME 0
#endif

// The headers define O_LARGEFILE as 0 on x86_64, but we need the
// actual value because it shows up in file flags.
#define O_LARGEFILE_REAL 00100000
Expand Down Expand Up @@ -712,11 +720,11 @@ class SandboxPolicyCommon : public SandboxPolicyBase {
}

virtual ResultExpr PrctlPolicy() const {
// Note: this will probably need PR_SET_VMA if/when it's used on
// Android without being overridden by an allow-all policy, and
// the constant will need to be defined locally.
Arg<int> op(0);
Arg<int> arg2(1);
return Switch(op)
.CASES((PR_SET_VMA), // Tagging of anonymous memory mappings
If(arg2 == PR_SET_VMA_ANON_NAME, Allow()).Else(InvalidSyscall()))
.CASES((PR_GET_SECCOMP, // BroadcastSetThreadSandbox, etc.
PR_SET_NAME, // Thread creation
PR_SET_DUMPABLE, // Crash reporting
Expand Down Expand Up @@ -2002,7 +2010,10 @@ class SocketProcessSandboxPolicy final : public SandboxPolicyCommon {

ResultExpr PrctlPolicy() const override {
Arg<int> op(0);
Arg<int> arg2(1);
return Switch(op)
.CASES((PR_SET_VMA), // Tagging of anonymous memory mappings
If(arg2 == PR_SET_VMA_ANON_NAME, Allow()).Else(InvalidSyscall()))
.CASES((PR_SET_NAME, // Thread creation
PR_SET_DUMPABLE, // Crash reporting
PR_SET_PTRACER), // Debug-mode crash handling
Expand Down Expand Up @@ -2093,7 +2104,10 @@ class UtilitySandboxPolicy : public SandboxPolicyCommon {

ResultExpr PrctlPolicy() const override {
Arg<int> op(0);
Arg<int> arg2(1);
return Switch(op)
.CASES((PR_SET_VMA), // Tagging of anonymous memory mappings
If(arg2 == PR_SET_VMA_ANON_NAME, Allow()).Else(InvalidSyscall()))
.CASES((PR_SET_NAME, // Thread creation
PR_SET_DUMPABLE, // Crash reporting
PR_SET_PTRACER, // Debug-mode crash handling
Expand Down

0 comments on commit 54b88de

Please sign in to comment.