xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/embed_file.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "sandboxed_api/embed_file.h"
16 
17 #include <fcntl.h>
18 #include <sys/stat.h>
19 #include <unistd.h>
20 
21 #include <string>
22 
23 #include "sandboxed_api/file_toc.h"
24 #include "absl/strings/str_cat.h"
25 #include "absl/synchronization/mutex.h"
26 #include "sandboxed_api/sandbox2/util.h"
27 #include "sandboxed_api/util/fileops.h"
28 #include "sandboxed_api/util/raw_logging.h"
29 
30 namespace sapi {
31 
32 namespace {
33 
34 #ifndef F_ADD_SEALS
35 #define F_ADD_SEALS 1033
36 #define F_SEAL_SEAL 0x0001
37 #define F_SEAL_SHRINK 0x0002
38 #define F_SEAL_GROW 0x0004
39 #define F_SEAL_WRITE 0x0008
40 #endif
41 
SealFile(int fd)42 bool SealFile(int fd) {
43   constexpr int kMaxRetries = 10;
44   for (int i = 0; i < kMaxRetries; ++i) {
45     if (fcntl(fd, F_ADD_SEALS,
46               F_SEAL_SEAL | F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE) == 0) {
47       return true;
48     }
49   }
50   return false;
51 }
52 
53 }  // namespace
54 
instance()55 EmbedFile* EmbedFile::instance() {
56   static auto* embed_file_instance = new EmbedFile();
57   return embed_file_instance;
58 }
59 
CreateFdForFileToc(const FileToc * toc)60 int EmbedFile::CreateFdForFileToc(const FileToc* toc) {
61   // Create a memfd/temp file and write contents of the SAPI library to it.
62   int fd = -1;
63   if (!sandbox2::util::CreateMemFd(&fd, toc->name)) {
64     SAPI_RAW_LOG(ERROR, "Couldn't create a temporary file for TOC name '%s'",
65                  toc->name);
66     return -1;
67   }
68   file_util::fileops::FDCloser embed_fd(fd);
69 
70   if (!file_util::fileops::WriteToFD(embed_fd.get(), toc->data, toc->size)) {
71     SAPI_RAW_PLOG(ERROR, "Couldn't write SAPI embed file '%s' to memfd file",
72                   toc->name);
73     return -1;
74   }
75 
76   // Make the underlying file non-writeable.
77   if (fchmod(embed_fd.get(),
78              S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1) {
79     SAPI_RAW_PLOG(ERROR, "Could't make FD=%d RX-only", embed_fd.get());
80     return -1;
81   }
82 
83   // Seal the file
84   if (!SealFile(embed_fd.get())) {
85     SAPI_RAW_PLOG(ERROR, "Couldn't apply file seals to FD=%d", embed_fd.get());
86     return -1;
87   }
88 
89   // Instead of working around problems with CRIU we reopen the file as
90   // read-only.
91   fd = open(absl::StrCat("/proc/", getpid(), "/fd/", embed_fd.get()).c_str(),
92             O_RDONLY | O_CLOEXEC);
93   if (fd == -1) {
94     SAPI_RAW_PLOG(ERROR, "Couldn't reopen '%d' read-only through /proc",
95                   embed_fd.get());
96     return -1;
97   }
98   return fd;
99 }
100 
GetFdForFileToc(const FileToc * toc)101 int EmbedFile::GetFdForFileToc(const FileToc* toc) {
102   // Access to file_tocs_ must be guarded.
103   absl::MutexLock lock{&file_tocs_mutex_};
104 
105   // If a file-descriptor for this toc already exists, just return it.
106   auto entry = file_tocs_.find(toc);
107   if (entry != file_tocs_.end()) {
108     SAPI_RAW_VLOG(3,
109                   "Returning pre-existing embed file entry for '%s', fd: %d "
110                   "(orig name: '%s')",
111                   toc->name, entry->second, entry->first->name);
112     return entry->second;
113   }
114 
115   int embed_fd = CreateFdForFileToc(toc);
116   if (embed_fd == -1) {
117     SAPI_RAW_LOG(ERROR, "Cannot create a file for FileTOC: '%s'", toc->name);
118     return -1;
119   }
120 
121   SAPI_RAW_VLOG(1, "Created new embed file entry for '%s' with fd: %d",
122                 toc->name, embed_fd);
123 
124   file_tocs_[toc] = embed_fd;
125   return embed_fd;
126 }
127 
GetDupFdForFileToc(const FileToc * toc)128 int EmbedFile::GetDupFdForFileToc(const FileToc* toc) {
129   int fd = GetFdForFileToc(toc);
130   if (fd == -1) {
131     return -1;
132   }
133   fd = dup(fd);
134   if (fd == -1) {
135     SAPI_RAW_PLOG(ERROR, "dup failed");
136   }
137   return fd;
138 }
139 
140 }  // namespace sapi
141