xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/sandbox2/notify.h (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1*ec63e07aSXin Li // Copyright 2019 Google LLC
2*ec63e07aSXin Li //
3*ec63e07aSXin Li // Licensed under the Apache License, Version 2.0 (the "License");
4*ec63e07aSXin Li // you may not use this file except in compliance with the License.
5*ec63e07aSXin Li // You may obtain a copy of the License at
6*ec63e07aSXin Li //
7*ec63e07aSXin Li //     https://www.apache.org/licenses/LICENSE-2.0
8*ec63e07aSXin Li //
9*ec63e07aSXin Li // Unless required by applicable law or agreed to in writing, software
10*ec63e07aSXin Li // distributed under the License is distributed on an "AS IS" BASIS,
11*ec63e07aSXin Li // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*ec63e07aSXin Li // See the License for the specific language governing permissions and
13*ec63e07aSXin Li // limitations under the License.
14*ec63e07aSXin Li 
15*ec63e07aSXin Li // The sandbox2::Notify class handless exceptional situations in the sandbox
16*ec63e07aSXin Li 
17*ec63e07aSXin Li #ifndef SANDBOXED_API_SANDBOX2_NOTIFY_H_
18*ec63e07aSXin Li #define SANDBOXED_API_SANDBOX2_NOTIFY_H_
19*ec63e07aSXin Li 
20*ec63e07aSXin Li #include <sys/types.h>
21*ec63e07aSXin Li 
22*ec63e07aSXin Li #include "absl/base/attributes.h"
23*ec63e07aSXin Li #include "absl/log/log.h"
24*ec63e07aSXin Li #include "sandboxed_api/sandbox2/comms.h"
25*ec63e07aSXin Li #include "sandboxed_api/sandbox2/result.h"
26*ec63e07aSXin Li #include "sandboxed_api/sandbox2/syscall.h"
27*ec63e07aSXin Li #include "sandboxed_api/sandbox2/util.h"
28*ec63e07aSXin Li 
29*ec63e07aSXin Li namespace sandbox2 {
30*ec63e07aSXin Li 
31*ec63e07aSXin Li enum ViolationType {
32*ec63e07aSXin Li   // A syscall disallowed by the policy was invoked.
33*ec63e07aSXin Li   kSyscallViolation,
34*ec63e07aSXin Li   // A syscall with cpu architecture not covered by the policy was invoked.
35*ec63e07aSXin Li   kArchitectureSwitchViolation,
36*ec63e07aSXin Li };
37*ec63e07aSXin Li 
38*ec63e07aSXin Li class Notify {
39*ec63e07aSXin Li  public:
40*ec63e07aSXin Li   virtual ~Notify() = default;
41*ec63e07aSXin Li 
42*ec63e07aSXin Li   // Called when a process has been created and executed, but not yet sandboxed.
43*ec63e07aSXin Li   // Using comms only makes sense if the client is sandboxed in the
44*ec63e07aSXin Li   // Executor::set_enable_sandbox_before_exec(false) mode.
45*ec63e07aSXin Li   // Returns a success indicator: false will cause the Sandbox Monitor to return
46*ec63e07aSXin Li   // sandbox2::Result::SETUP_ERROR for Run()/RunAsync().
EventStarted(pid_t pid,Comms * comms)47*ec63e07aSXin Li   virtual bool EventStarted(pid_t pid, Comms* comms) { return true; }
48*ec63e07aSXin Li 
49*ec63e07aSXin Li   // Called when all sandboxed processes finished.
EventFinished(const Result & result)50*ec63e07aSXin Li   virtual void EventFinished(const Result& result) {}
51*ec63e07aSXin Li 
52*ec63e07aSXin Li   // Called when a process exited with a syscall violation.
EventSyscallViolation(const Syscall & syscall,ViolationType type)53*ec63e07aSXin Li   virtual void EventSyscallViolation(const Syscall& syscall,
54*ec63e07aSXin Li                                      ViolationType type) {}
55*ec63e07aSXin Li 
56*ec63e07aSXin Li   // Called when a policy called TRACE. The syscall is allowed and logged if
57*ec63e07aSXin Li   // this method returns true. This allows for implementing 'log, but allow'
58*ec63e07aSXin Li   // policies.
59*ec63e07aSXin Li   ABSL_DEPRECATED("Override EventSyscallTrace() instead")
EventSyscallTrap(const Syscall & syscall)60*ec63e07aSXin Li   virtual bool EventSyscallTrap(const Syscall& syscall) { return false; }
61*ec63e07aSXin Li 
62*ec63e07aSXin Li   // Actions to perform after calling EventSyscallTrace.
63*ec63e07aSXin Li   enum class TraceAction {
64*ec63e07aSXin Li     // Deny the syscall.
65*ec63e07aSXin Li     kDeny,
66*ec63e07aSXin Li     // Allow the syscall.
67*ec63e07aSXin Li     kAllow,
68*ec63e07aSXin Li     // Allow the syscall so its return value can be inspected through a
69*ec63e07aSXin Li     // subsequent call to EventSyscallReturn.
70*ec63e07aSXin Li     // Requires Linux kernel 4.8 or later.
71*ec63e07aSXin Li     kInspectAfterReturn
72*ec63e07aSXin Li   };
73*ec63e07aSXin Li 
74*ec63e07aSXin Li   // Called when a policy called TRACE. The syscall is allowed or denied
75*ec63e07aSXin Li   // depending on the return value of this function.
EventSyscallTrace(const Syscall & syscall)76*ec63e07aSXin Li   virtual TraceAction EventSyscallTrace(const Syscall& syscall) {
77*ec63e07aSXin Li     if (EventSyscallTrap(syscall)) {
78*ec63e07aSXin Li       LOG(WARNING) << "[PERMITTED]: SYSCALL ::: PID: " << syscall.pid()
79*ec63e07aSXin Li                    << ", PROG: '" << util::GetProgName(syscall.pid())
80*ec63e07aSXin Li                    << "' : " << syscall.GetDescription();
81*ec63e07aSXin Li       return TraceAction::kAllow;
82*ec63e07aSXin Li     }
83*ec63e07aSXin Li     return TraceAction::kDeny;
84*ec63e07aSXin Li   }
85*ec63e07aSXin Li 
86*ec63e07aSXin Li   // Called when a policy called TRACE and EventSyscallTrace returned
87*ec63e07aSXin Li   // kInspectAfterReturn.
EventSyscallReturn(const Syscall & syscall,int64_t return_value)88*ec63e07aSXin Li   virtual void EventSyscallReturn(const Syscall& syscall,
89*ec63e07aSXin Li                                   int64_t return_value) {}
90*ec63e07aSXin Li 
91*ec63e07aSXin Li   // Called when a process received a signal.
EventSignal(pid_t pid,int sig_no)92*ec63e07aSXin Li   virtual void EventSignal(pid_t pid, int sig_no) {}
93*ec63e07aSXin Li };
94*ec63e07aSXin Li 
95*ec63e07aSXin Li }  // namespace sandbox2
96*ec63e07aSXin Li 
97*ec63e07aSXin Li #endif  // SANDBOXED_API_SANDBOX2_NOTIFY_H_
98