|
22 | 22 | #include <fstream>
|
23 | 23 | #include <filesystem>
|
24 | 24 |
|
| 25 | +#ifdef MIR_USE_APPARMOR |
| 26 | +#include <sys/apparmor.h> |
| 27 | +#endif |
| 28 | + |
25 | 29 | namespace mf = mir::frontend;
|
26 | 30 |
|
27 | 31 | namespace
|
@@ -89,9 +93,10 @@ std::string mf::DesktopFileManager::resolve_app_id(const scene::Surface* surface
|
89 | 93 |
|
90 | 94 | auto session = surface->session().lock();
|
91 | 95 | auto pid = session->process_id();
|
| 96 | + auto socket_fd = session->socket_fd(); |
92 | 97 |
|
93 | 98 | // Fourth, check if the window belongs to snap
|
94 |
| - found = resolve_if_snap(pid); |
| 99 | + found = resolve_if_snap(pid, socket_fd); |
95 | 100 | if (found)
|
96 | 101 | return found->id;
|
97 | 102 |
|
@@ -157,8 +162,31 @@ std::string mf::DesktopFileManager::parse_snap_security_profile_to_desktop_id(st
|
157 | 162 | return sandboxed_app_id + DESKTOP_FILE_POSTFIX;
|
158 | 163 | }
|
159 | 164 |
|
160 |
| -std::shared_ptr<mf::DesktopFile> mf::DesktopFileManager::resolve_if_snap(int pid) |
| 165 | +std::shared_ptr<mf::DesktopFile> mf::DesktopFileManager::resolve_if_snap(int pid, mir::Fd const& socket_fd) |
161 | 166 | {
|
| 167 | +#ifdef MIR_USE_APPARMOR |
| 168 | + // First, try to resolve the AppArmor profile |
| 169 | + char* label_cstr; |
| 170 | + char* mode_cstr; |
| 171 | + |
| 172 | + if (aa_getpeercon(socket_fd, &label_cstr, &mode_cstr) >= 0) |
| 173 | + { |
| 174 | + std::string const label{label_cstr}; |
| 175 | + free(label_cstr); |
| 176 | + // mode_cstr should NOT be freed, as it's from the same buffer as label_cstr |
| 177 | + |
| 178 | + auto sandboxed_app_id = parse_snap_security_profile_to_desktop_id(label); |
| 179 | + if (!sandboxed_app_id.empty()) |
| 180 | + { |
| 181 | + if (auto file = cache->lookup_by_app_id(sandboxed_app_id)) |
| 182 | + return file; |
| 183 | + } |
| 184 | + } |
| 185 | +#else |
| 186 | + (void)socket_fd; |
| 187 | +#endif |
| 188 | + |
| 189 | + // If that fails, try to read /proc/<PID>/attr/current |
162 | 190 | std::string attr_file = "/proc/" + std::to_string(pid) + "/attr/current";
|
163 | 191 | if (!std::filesystem::exists(attr_file))
|
164 | 192 | return nullptr;
|
|
0 commit comments