Debian Codegraph @

docs/design/secure-config.md

raw
1 # Secure JJ config
2
3 Author: [Matt Stark](mailto:[email protected])
4
5 ## The problem
6
7 An attacker that has control over your jj configuration has full control over
8 your system when you run specific commands. As an example, an attacker can have
9 you enable the following repo config:
10
11 ```
12 [fix.tools.foo]
13 command = ["malicious", "command"]
14 ```
15
16 When a user then runs `jj fix`, this will run their malicious command and they
17 can gain full control over your system. This can be achieved via zipping up a
18 repo and sending it to the user, with the `.jj/repo/config.toml` file containing
19 the above config (hence why this is colloquially known as the “zip file
20 problem”).
21
22 There are plans to add features such as hooks to jj which will only make it
23 easier for this to occur. For simplicity’s sake, we will assume that if an
24 attacker has their configuration enabled on your system, it is compromised.
25
26 Assume any reference to repo config can equivalently be replaced with workspace
27 configs. We will treat them in the same way.
28
29 ### Threat model
30
31 This is not something that can be 100% defended against. Defense against all
32 possible attack vectors is infeasible, so we will instead note all the attack
33 vectors and what it would take to defend against them.
34
35 #### Attack vector 1: No-knowledge attacker
36
37 1. The attacker creates a repo
38 2. The attacker runs
39 `jj config set --repo fix.tools.foo ‘[“malicious”, “command”]’`
40 4. The attacker zips up their repo and sends it to the victim
41 5. The victim unzips the repo, make some changes, then run `jj fix`
42 6. They have now executed an arbitrary command on the victim’s system
43
44 This attack vector can be solved by ensuring that we can determine the user who
45 created the repo.
46
47 #### Attack vector 2: Basic replay attack
48
49 1. The victim uploads a zip file of a repository they have locally on their
50 system
51 2. The attacker can now see any configs / config references stored in the
52 repository
53 3. The attacker runs
54 `jj config set --repo fix.tools.foo ‘[“malicious”, “command”]’`
55 4. The attacker copies the victim’s cryptographic signature and puts it in
56 their malicious repository.
57 5. The attacker zips up their repo and sends it to the victim
58 6. The victim unzips the repo at an arbitrary location, make some changes, then
59 run `jj fix`
60 7. They have now executed an arbitrary command on the victim’s system
61
62 This attack vector can be solved by ensuring that we can determine the path that
63 the repo was stored at.
64
65 #### Attack vector 3: Replay attack with social engineering to preserve paths
66
67 1. The victim uploads a zip file of a repository they have locally on their
68 system at `/path/to/repo`
69 2. The attacker can now see any configs / config references stored in the
70 repository
71 3. The attacker runs
72 `jj config set --repo fix.tools.foo ‘[“malicious”, “command”]’`
73 4. The attacker copies the victim’s cryptographic signature and puts it in
74 their malicious repository.
75 5. The attacker zips up their repo, sends it to the victim, and instructs them
76 to install it at `/path/to/repo`
77 6. The victim unzips the repo at `/path/to/repo`, make some changes, then run
78 `jj fix`
79 7. They have now executed an arbitrary command on the victim’s system
80
81 This attack vector can be solved by making repository configuration
82 untamperable.
83
84 #### Attack vector 4: Extremely advanced replay attack with insecure code
85
86 1. The victim creates a repo
87 2. The victim runs `jj config set --repo fix.tools.foo = [“$repo/format.py”]`
88 3. The victim uploads a zip file of a repository they have locally on their
89 system at `/path/to/repo`
90 4. The attacker can now see any configs / config references stored in the
91 repository
92 5. The attacker modifies `format.py` to be malicious
93 6. The attacker zips up their repo and sends it to the victim
94 7. The victim runs `jj fix`
95 8. They have now executed an arbitrary command on the victim’s system
96
97 This attack vector cannot feasibly be dealt with. It would require a signature
98 of the transitive closure of files that can be accessed via jj configs to solve.
99
100 ## Objective
101
102 ### Goals
103
104 * Prevent as many of the above attack vectors as possible
105 * Have minimal negative impacts on UX
106
107 ### Non-goals (Optional)
108
109 * Use strategies such as sandboxing to mitigate damage
110 * We could do this for formatters, for example, but then repo hooks would
111 have the same problem
112 * These options are not mutually exclusive
113
114 ## Detailed Design
115
116 Note that this design uses the word "repo" for everything, but we will use
117 precisely the same technique for workspace configs.
118
119 ### Storing config out-of-repo
120
121 We start by creating the concept of a "config ID".
122 * A config ID is a randomly generated ID used to identify a configuration.
123 * It should be stored in the repo
124 * It should uniquely identify a single repository / workspace
125
126 For clarity's sake, we will call these "config IDs" for repos, and "workspace
127 config IDs" for workspaces.
128
129 We will store per-repo configuration in
130 `etcetera::BaseStrategy::config_dir().join(“jj”).join(“repos”).join(config_id)`.
131
132 The filesystem structure will look like:
133
134 ```
135 $HOME/.config/jj/
136 repos/
137 abc123/
138 metadata.binpb
139 config.toml
140 workspaces/
141 def456/
142 config.toml
143 metadata.binpb
144 my-repo/.jj/
145 workspace-config-id (contains "def456")
146 workspace-config.toml (unused by jj, details below)
147 repo/
148 config-id (contains "abc123")
149 config.toml (unused by jj, details below)
150 ```
151
152 `metadata.binpb` will refer to the following protobuf:
153
154 ```proto
155 message Metadata {
156 // This is used to distinguish between copies and moves.
157 string path = 1;
158 }
159 ```
160
161 The function to load repository configuration, will roughly speaking, look like:
162
163 ```rust
164 enum ConfigLoadError { NoRepoId, NoConfig, PathMismatch, }
165
166 fn load_repo_config_path(repo: &Path) -> Result<PathBuf, ConfigLoadError> {
167 let config_id = std::fs::read_to_string(repo.join("config-id"))
168 .map_err(|_|Err(NoRepoId))?;
169 let repo_config_dir = config_dir.join("repos").join(config_id);
170 let metadata = Metadata::decode(
171 std::fs::read(repo_config_dir.join("metadata.binpb")) .map_err(|_|
172 Err(NoConfig))? )?;
173 if metadata.path != repo {
174 return Err(PathMismatch)
175 }
176 Ok(repo_config_dir.join("config.toml"))
177 }
178 ```
179
180 #### Happy path
181
182 Normally we will simply:
183 1. Load the `config-id` file
184 2. Check that `$HOME/.config/jj/repos/$CONFIG_ID` exists
185 3. Validate that the path in `$HOME/.config/jj/repos/$CONFIG_ID/metadata.binpb`
186 matches the repo's path
187 4. Find the corresponding config in
188 `$HOME/.config/jj/repos/$CONFIG_ID/config.toml`
189 5. Load that config.
190
191 However, these steps can fail. The following sections are how we will handle
192 the errors.
193
194 #### No config-ID file
195
196 ##### Without config files
197
198 If `$repo/.jj/repo/config.toml` does not exist, the repo doesn't have any config,
199 and thus requires no config ID.
200
201 Note that the expected way to generate a config-ID would be for the user to either
202 run `jj config edit` or `jj config path`.
203
204 ##### With config files
205
206 If the config file exists, then this corresponds to a legacy repo. To preserve
207 backwards compatibility, we will introduce a period of auto-migration. The
208 current plan is 12 jj versions (approximately 1 year). During this period, if a
209 config ID has not yet been generated, we will silently perform the following
210 (order matters, to ensure failure halfway through doesn’t affect things):
211
212 1. Generate a config ID `abc123`
213 2. Create `$HOME/.config/jj/repo/abc123/metadata.binpb`
214 3. Create `$HOME/.config/jj/repo/abc123/config.toml` as a copy of the original
215 config file
216 4. Atomically generate a `config-id` file containing `abc123`
217 5. Remove the original config file
218 6. For the user's convenience, and for older version of jj, we:
219 * Try to symlink the old config to the new config
220 * If this fails (symlinks don't play nice on windows), we replace it with
221 the same file content, with an extra comment at the top telling the user
222 not to edit the file, and set it to readonly.
223
224 After the migration period is over, we will:
225 * Stop the auto-migration
226 * If the config file was previously created by auto-migration, delete it
227 * `zip` follows symlinks by default, so this would reveal the content of your
228 config to an attacker if they convinced you to send them your repo.
229 * If the config file exists, print a warning that the config file has been ignored.
230
231 #### No config directory for the config-ID
232
233 This could occur, for example, if the user created a repository in linux,
234 rebooted into windows on the same computer, and attempted to access that repo.
235
236 In this event, the user probably expects their config to be attached to the
237 repository, and they expect it to still work on linux, so we will:
238 * Create a new `$HOME/.config/jj/repo/$CONFIG_ID` directory for the same config
239 ID (to ensure that the config still works on windows)
240 * Print a warning that if there was any per-repo configuration, it is no longer
241 available.
242
243 #### Distinguishing copies from moves
244
245 A user's expectation is that if they run `cp -r old_repo new_repo`, then modify
246 the old repo's config, the new repo's config is not affected. Thus, we need to
247 make sure that the repo remains in a 1:1 relationship with the config. Multiple
248 repos should not point to the same config.
249
250 To achieve this, we point the repo at the config, and the config back at the
251 repo. If the path stored in the config doesn't match, we know that something has
252 happened. To decide precisely what happened:
253 * If the path stored in the config no longer exists, we assume it's a move
254 * We update `metadata.path` to point to the new path
255 * Otherwise, we attempt to write to a temporary file in `$NEW_REPO/.jj/repo`.
256 * If it shows up in the original repository, it's some kind of reference
257 (link, mount, etc.), so we don't do anything.
258 * Otherwise, it's a copy, so we generate a new config ID and copy the old
259 config to it
260
261 Note that with a copy, we only actually copy the config when the user runs a
262 `jj` command. This means that you can end up in a situation where you:
263 1. `cp -r old_repo new_repo`
264 2. In `old_repo`, `jj config set --repo`
265 3. Run a jj command in the copy. This copies the config, including the config
266 from step 2.
267
268 However, this is inherent to storing config out of the repo and is thus
269 unavoidable.
270
271 ### Garbage collection
272
273 There is `jj config gc` command to delete configuration for repos that
274 are deleted/moved. The command asks for user approval because of the
275 following considerations:
276
277 * It is impossible to distinguish "deleted" from "moved".
278 * If you have something like a chroot or a dual boot where you share the
279 config, you may have references to config IDs with a different path.
280
281 For safety, the command only removes the well-known files it manages
282 (`config.toml` and `metadata.binpb`) and then removes the per-repo
283 directory non-recursively. If a user (or another tool) has placed an
284 unrelated file inside the directory, the directory is left in place and
285 a warning is printed instead.
286
287 ### Attack vectors remaining
288
289 Unfortunately, there is no way to distinguish copying / moving from a replay
290 attack. The attacker, if they know a config ID that exists on your system, can
291 create a repo with the same config ID. However, the fact that the config itself is
292 stored out of repo inherently prevents simple replay attacks. In order for the
293 attacker to exploit this, they would need to:
294
295 * Know your config ID (requires uploading a zip file or something similar)
296 * Get lucky by the victim having a “risky” per-repo config
297 * Eg. fix.tools pointing to `$repo/formatter`
298 * Know how to exploit it
299 * Because your config file is stored out-of-repo, the attacker will likely not
300 know any of this without some social engineering
301
302 Given the impossibility of distinguishing copying / moving from a replay attack,
303 any security measures we come up with to deal with this would have false positives
304 whenever you do a copy / move, creating a significant UX cost. Thus, we
305 intentionally choose not to deal with this kind of attack in the initial
306 version, and have no current intention to solve it in future versions either.
307
308 We could potentially deal with this attack vector as an opt-in feature in the
309 future, but it has dubious benefit, as the kind of user who would opt in to
310 something like this is also the kind of user who would never upload their repo
311 as a zip file.
312
313 Because only the config-id is stored in-repo, the only attack vector remaining
314 is the replay attack I mentioned above.
315
316 ### UX issues
317
318 * Copying the repo is essentially a symlink to an old config until you update
319 it
320 * Multiple users on the same system would each have different per-repo configs
321 * This can be solved by simply symlinking `$HOME/.config/jj` to
322 `%APPDATA%/jj` (or vice versa) to solve this issue. You were probably
323 doing this anyway with specifically the user config file instead of the
324 directory.
325 * The repo config will no longer be available across machines if the user is
326 using something like a distributed file system. This is probably OK, since
327 if the user has a complex setup like this, they will also have issues with
328 the config of every other application not being shared, and could easily
329 solve this by sharing `~/.config`.
330
331 ## Alternatives considered
332
333 ### Store the configs in-repo with an untamperable cryptographic signature
334
335 There are a few questions we would need to resolve here, all with significant
336 drawbacks:
337
338 ##### Do we include paths in the signature?
339
340 If we do, we introduce a whole bunch of additional annoying UX to the user when
341 they move repos around.
342
343 If we don't, we leave ourselved exposed to additional attack vectors
344
345 ##### How to sign the content of the repo config?
346
347 * We could not sign it at all, but that would leave ourselves exposed to
348 additional attack vectors.
349 * We could sign the content of the repo config, but then when the user
350 manually edits the file we have additional UX we need to introduce.
351 * We could store both the content and the signature in the repo protobuf, but
352 the config would no longer exist on disk as a regular file, and thus you
353 couldn't use standard tools to read and write the config.
354
355 All of these options were discussed in the original PR (#7761), which, unlike
356 the current approach, introduced user interventions and a review process. The
357 current approach, on the other hand, while it does have some extremely minor UX
358 weirdness, has no such issues.