File: render_process_host_sandbox_policy_win_browsertest.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (136 lines) | stat: -rw-r--r-- 4,962 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "base/values.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_base.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/shell/browser/shell.h"
#include "net/dns/mock_host_resolver.h"
#include "sandbox/policy/features.h"
#include "sandbox/policy/win/sandbox_win.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace content {

// Test class to verify that the RendererAppContainer feature works correctly.
class RendererAppContainerFeatureBrowserTest : public ContentBrowserTest {
 public:
  RendererAppContainerFeatureBrowserTest() {
    scoped_feature_list_.InitWithFeatureState(
        sandbox::policy::features::kRendererAppContainer, true);
  }

  void SetUpOnMainThread() override {
    // Support multiple sites on the test server.
    host_resolver()->AddRule("*", "127.0.0.1");
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

IN_PROC_BROWSER_TEST_F(RendererAppContainerFeatureBrowserTest, Navigate) {
  ASSERT_TRUE(embedded_test_server()->Start());
  EXPECT_TRUE(NavigateToURL(
      shell(), embedded_test_server()->GetURL("foo.com", "/title1.html")));
}

// Test class to verify the behavior of the pipe interceptions for renderers.
class PipeLockdownFeatureBrowserTest : public ContentBrowserTest {
 public:
  PipeLockdownFeatureBrowserTest() = default;

  void SetUpOnMainThread() override {
    // Support multiple sites on the test server.
    host_resolver()->AddRule("*", "127.0.0.1");
  }

 protected:
  WebContentsImpl* contents() const {
    return static_cast<WebContentsImpl*>(shell()->web_contents());
  }
};

IN_PROC_BROWSER_TEST_F(PipeLockdownFeatureBrowserTest, Navigate) {
  ASSERT_TRUE(embedded_test_server()->Start());
  EXPECT_TRUE(NavigateToURL(
      shell(), embedded_test_server()->GetURL("foo.com", "/title1.html")));

  // Multiple renderer processes might have started. It is safe to hold this Pid
  // here because no renderers can start or stop while on the UI thread.
  base::ProcessId renderer_process_id =
      contents()->GetPrimaryMainFrame()->GetProcess()->GetProcess().Pid();

  base::RunLoop run_loop;
  base::Value out_args;
  sandbox::policy::SandboxWin::GetPolicyDiagnostics(
      base::BindLambdaForTesting([&run_loop, &out_args](base::Value args) {
        out_args = std::move(args);
        run_loop.Quit();
      }));
  run_loop.Run();

  const base::Value::List* process_list = out_args.GetIfList();
  ASSERT_TRUE(process_list);
  bool found_renderer = false;
  bool found_chrome_pipe_create_pipe_rule = false;
  bool found_chrome_pipe_open_rule = false;
  bool found_chrome_sync_pipe_create_pipe_rule = false;
  bool found_chrome_sync_pipe_open_rule = false;
  for (const base::Value& process_value : *process_list) {
    const base::Value::Dict* process = process_value.GetIfDict();
    ASSERT_TRUE(process);
    absl::optional<double> pid = process->FindDouble("processId");
    ASSERT_TRUE(pid.has_value());
    if (base::checked_cast<base::ProcessId>(pid.value()) != renderer_process_id)
      continue;
    found_renderer = true;
    auto* rules = process->FindDict("policyRules");
    ASSERT_TRUE(rules);

    const base::Value::List* open_file_opcodes = rules->FindList("NtOpenFile");
    if (open_file_opcodes) {
      for (const base::Value& opcode : *open_file_opcodes) {
        auto* value = opcode.GetIfString();
        ASSERT_TRUE(value);
        if (value->find("chrome.'") != value->npos)
          found_chrome_pipe_open_rule = true;
        if (value->find("chrome.sync.'") != value->npos)
          found_chrome_sync_pipe_open_rule = true;
      }
    }

    const base::Value::List* create_named_pipe_opcodes =
        rules->FindList("CreateNamedPipeW");
    if (create_named_pipe_opcodes) {
      for (const base::Value& opcode : *create_named_pipe_opcodes) {
        auto* value = opcode.GetIfString();
        ASSERT_TRUE(value);
        if (value->find("chrome.'") != value->npos)
          found_chrome_pipe_create_pipe_rule = true;
        if (value->find("chrome.sync.'") != value->npos)
          found_chrome_sync_pipe_create_pipe_rule = true;
      }
    }
  }

  EXPECT_TRUE(found_renderer);

  // There should never be an NtOpenFile rule for chrome.sync.*.
  EXPECT_FALSE(found_chrome_sync_pipe_open_rule);

  // There should never be a way to Create pipes for chrome.*.
  EXPECT_FALSE(found_chrome_pipe_create_pipe_rule);

  // With pipe lockdown enabled, no pipe rules should exist for renderers.
  EXPECT_FALSE(found_chrome_sync_pipe_create_pipe_rule);
  EXPECT_FALSE(found_chrome_pipe_open_rule);
}

}  // namespace content