File: browser_updater_client_util_mac.mm

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (330 lines) | stat: -rw-r--r-- 12,335 bytes parent folder | download | duplicates (3)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/updater/browser_updater_client_util.h"

#include <Foundation/Foundation.h>
#import <OpenDirectory/OpenDirectory.h>
#import <ServiceManagement/ServiceManagement.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

#include <optional>

#include "base/apple/bridging.h"
#include "base/apple/bundle_locations.h"
#include "base/apple/foundation_util.h"
#include "base/command_line.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/logging.h"
#include "base/mac/authorization_util.h"
#include "base/mac/scoped_authorizationref.h"
#include "base/memory/scoped_refptr.h"
#include "base/process/launch.h"
#include "base/process/process.h"
#include "base/strings/strcat.h"
#include "base/strings/sys_string_conversions.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/threading/scoped_blocking_call.h"
#include "base/time/time.h"
#include "build/buildflag.h"
#include "chrome/browser/updater/browser_updater_client.h"
#include "chrome/browser/updater/browser_updater_client_util.h"
#include "chrome/browser/updater/browser_updater_helper_client_mac.h"
#include "chrome/common/chrome_version.h"
#include "chrome/grit/branded_strings.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/updater/constants.h"
#include "chrome/updater/updater_scope.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/l10n/l10n_util_mac.h"

namespace {

constexpr char kInstallCommand[] = "install";

base::FilePath GetUpdaterExecutablePath() {
  return base::FilePath(base::StrCat({kUpdaterName, ".app"}))
      .Append(FILE_PATH_LITERAL("Contents"))
      .Append(FILE_PATH_LITERAL("MacOS"))
      .Append(kUpdaterName);
}

std::optional<uid_t> GetBundleOwner() {
  const base::FilePath path = base::apple::OuterBundlePath();
  base::stat_wrapper_t stat_info = {};
  if (base::File::Lstat(path, &stat_info) != 0) {
    VPLOG(2) << "Failed to get information on path " << path.value();
    return std::nullopt;
  }

  if (S_ISLNK(stat_info.st_mode)) {
    VLOG(2) << "Path " << path.value() << " is a symbolic link.";
    return std::nullopt;
  }

  return stat_info.st_uid;
}

bool IsEffectiveUserAdmin() {
  NSError* error;
  ODNode* search_node = [ODNode nodeWithSession:[ODSession defaultSession]
                                           type:kODNodeTypeLocalNodes
                                          error:&error];
  if (!search_node) {
    VLOG(2) << "Error creating ODNode: " << search_node;
    return false;
  }
  ODQuery* query =
      [ODQuery queryWithNode:search_node
              forRecordTypes:kODRecordTypeUsers
                   attribute:kODAttributeTypeUniqueID
                   matchType:kODMatchEqualTo
                 queryValues:[NSString stringWithFormat:@"%d", geteuid()]
            returnAttributes:kODAttributeTypeStandardOnly
              maximumResults:1
                       error:&error];
  if (!query) {
    VLOG(2) << "Error constructing query: " << error;
    return false;
  }

  NSArray<ODRecord*>* results = [query resultsAllowingPartial:NO error:&error];
  if (!results) {
    VLOG(2) << "Error executing query: " << error;
    return false;
  }

  ODRecord* admin_group = [search_node recordWithRecordType:kODRecordTypeGroups
                                                       name:@"admin"
                                                 attributes:nil
                                                      error:&error];
  if (!admin_group) {
    VLOG(2) << "Failed to get 'admin' group: " << error;
    return false;
  }

  bool result = [admin_group isMemberRecord:results.firstObject error:&error];
  VLOG_IF(2, error) << "Failed to get member record: " << error;

  return result;
}

bool ShouldPromoteUpdater() {
  std::optional<uid_t> owner = GetBundleOwner();

  // 1) Should promote if browser is owned by root and not installed. The not
  // installed part of this case is handled in version_updater_mac.mm
  if (owner && *owner == 0) {
    return true;
  }

  // 2) If the effective user is root and the browser is not owned by root (i.e.
  // if the current user has run with sudo).
  if (geteuid() == 0) {
    return true;
  }

  // 3) If effective user is not the owner of the browser and is an
  // administrator.
  return owner && *owner != geteuid() && IsEffectiveUserAdmin();
}

int RunCommand(const base::FilePath& exe_path, const char* cmd_switch) {
  base::CommandLine command(exe_path);
  command.AppendSwitch(cmd_switch);
  command.AppendSwitch(updater::kEnableLoggingSwitch);
  command.AppendSwitchASCII(updater::kLoggingModuleSwitch,
                            updater::kLoggingModuleSwitchValue);

  int exit_code = -1;
  auto process = base::LaunchProcess(command, {});
  if (!process.IsValid())
    return exit_code;

  process.WaitForExitWithTimeout(base::Seconds(120), &exit_code);

  return exit_code;
}

// Only works in kUser scope.
void RegisterBrowser(base::OnceClosure complete) {
  BrowserUpdaterClient::Create(updater::UpdaterScope::kUser)
      ->Register(std::move(complete));
}

// Only works in kUser scope.
void InstallUpdaterAndRegisterBrowser(base::OnceClosure complete) {
  base::ThreadPool::PostTaskAndReplyWithResult(
      FROM_HERE,
      {base::MayBlock(), base::WithBaseSyncPrimitives(),
       base::TaskPriority::BEST_EFFORT,
       base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN},
      base::BindOnce([]() {
        // The updater executable should be in
        // BRANDING.app/Contents/Frameworks/BRANDING.framework/Versions/V/
        // Helpers/Updater.app/Contents/MacOS/Updater
        const base::FilePath updater_executable_path =
            base::apple::FrameworkBundlePath()
                .Append(FILE_PATH_LITERAL("Helpers"))
                .Append(GetUpdaterExecutablePath());

        if (!base::PathExists(updater_executable_path)) {
          VLOG(1) << "The updater does not exist in the bundle.";
          return false;
        }

        int exit_code = RunCommand(updater_executable_path, kInstallCommand);
        if (exit_code != 0) {
          VLOG(1) << "Couldn't install the updater. Exit code: " << exit_code;
          return false;
        }
        return true;
      }),
      base::BindOnce(
          [](base::OnceClosure complete, bool success) {
            if (success) {
              RegisterBrowser(std::move(complete));
            } else {
              std::move(complete).Run();
            }
          },
          std::move(complete)));
}

// Marks the browser as active, and schedules a call 1 hour later to mark the
// browser as active again.
void SetActive() {
  base::FilePath actives_dir =
      base::GetHomeDir()
          .AppendASCII("Library")
          .Append(FILE_PATH_LITERAL(COMPANY_SHORTNAME_STRING))
          .Append(FILE_PATH_LITERAL(COMPANY_SHORTNAME_STRING "SoftwareUpdate"))
          .AppendASCII("Actives");
  if (!CreateDirectory(actives_dir)) {
    return;
  }
  base::WriteFile(actives_dir.Append(base::apple::BaseBundleID()), "");
  base::ThreadPool::PostDelayedTask(
      FROM_HERE,
      {base::MayBlock(), base::TaskPriority::BEST_EFFORT,
       base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN},
      base::BindOnce(&SetActive), base::Hours(1));
}

}  // namespace

std::string CurrentlyInstalledVersion() {
  base::ScopedBlockingCall blocks(FROM_HERE, base::BlockingType::WILL_BLOCK);
  base::FilePath outer_bundle = base::apple::OuterBundlePath();
  base::FilePath plist_path =
      outer_bundle.Append("Contents").Append("Info.plist");
  NSDictionary* info_plist = [NSDictionary
      dictionaryWithContentsOfFile:base::apple::FilePathToNSString(plist_path)];
  return base::SysNSStringToUTF8(base::apple::ObjCCast<NSString>(
      info_plist[@"CFBundleShortVersionString"]));
}

updater::UpdaterScope GetBrowserUpdaterScope() {
  std::optional<uid_t> owner = GetBundleOwner();
  return owner && (*owner == 0 || *owner != geteuid())
             ? updater::UpdaterScope::kSystem
             : updater::UpdaterScope::kUser;
}

void EnsureUpdater(base::OnceClosure prompt, base::OnceClosure complete) {
  base::ThreadPool::PostTask(FROM_HERE,
                             {base::MayBlock(), base::TaskPriority::BEST_EFFORT,
                              base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN},
                             base::BindOnce(&SetActive));
  base::ThreadPool::PostTaskAndReplyWithResult(
      FROM_HERE,
      {base::MayBlock(), base::TaskPriority::BEST_EFFORT,
       base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN},
      base::BindOnce(&GetBrowserUpdaterScope),
      base::BindOnce(
          [](base::OnceClosure prompt, base::OnceClosure complete,
             updater::UpdaterScope scope) {
            scoped_refptr<BrowserUpdaterClient> client =
                BrowserUpdaterClient::Create(scope);
            client->IsBrowserRegistered(base::BindOnce(
                [](scoped_refptr<BrowserUpdaterClient> client,
                   base::OnceClosure prompt, base::OnceClosure complete,
                   bool registered) {
                  if (registered) {
                    std::move(complete).Run();
                    return;
                  }
                  base::ThreadPool::PostTaskAndReplyWithResult(
                      FROM_HERE, {base::MayBlock()},
                      base::BindOnce(&ShouldPromoteUpdater),
                      base::BindOnce(
                          [](scoped_refptr<BrowserUpdaterClient> client,
                             base::OnceClosure prompt,
                             base::OnceClosure complete, bool promote) {
                            if (promote) {
                              // User intervention is required; prompt.
                              std::move(prompt).Run();
                              std::move(complete).Run();
                              return;
                            }
                            // Check whether an updater exists.
                            client->GetUpdaterVersion(base::BindOnce(
                                [](base::OnceClosure complete,
                                   const base::Version& version) {
                                  if (!version.IsValid()) {
                                    InstallUpdaterAndRegisterBrowser(
                                        std::move(complete));
                                  } else {
                                    RegisterBrowser(std::move(complete));
                                  }
                                },
                                std::move(complete)));
                          },
                          client, std::move(prompt), std::move(complete)));
                },
                client, std::move(prompt), std::move(complete)));
          },
          std::move(prompt), std::move(complete)));
}

void SetupSystemUpdater() {
  NSString* prompt = l10n_util::GetNSStringFWithFixup(
      IDS_PROMOTE_AUTHENTICATION_PROMPT,
      l10n_util::GetStringUTF16(IDS_PRODUCT_NAME));
  base::mac::ScopedAuthorizationRef authorization =
      base::mac::AuthorizationCreateToRunAsRoot(
          base::apple::NSToCFPtrCast(prompt));
  if (!authorization.get()) {
    VLOG(0) << "Could not get authorization to run as root.";
    return;
  }

  base::apple::ScopedCFTypeRef<CFErrorRef> error;
  Boolean result =
      SMJobBless(kSMDomainSystemLaunchd,
                 base::SysUTF8ToCFStringRef(kPrivilegedHelperName).get(),
                 authorization, error.InitializeInto());
  if (!result) {
    base::apple::ScopedCFTypeRef<CFStringRef> desc(
        CFErrorCopyDescription(error.get()));
    VLOG(0) << "Could not bless the privileged helper. Resulting error: "
            << base::SysCFStringRefToUTF8(desc.get());
    return;
  }

  base::MakeRefCounted<BrowserUpdaterHelperClientMac>()->SetupSystemUpdater(
      base::BindOnce([](int result) {
        VLOG_IF(1, result != 0)
            << "There was a problem with performing the system "
               "updater tasks. Result: "
            << result;
      }));
}