Skip to content

Commit 7891e46

Browse files
peffgitster
authored andcommitted
gpg-interface: set trust level of missing key to "undefined"
In check_signature(), we initialize the trust_level field to "-1", with the idea that if gpg does not return a trust level at all (if there is no signature, or if the signature is made by an unknown key), we'll use that value. But this has two problems: 1. Since the field is an enum, it's up to the compiler to decide what underlying storage to use, and it only has to fit the values we've declared. So we may not be able to store "-1" at all. And indeed, on my system (linux with gcc), the resulting enum is an unsigned 32-bit value, and -1 becomes 4294967295. The difference may seem academic (and you even get "-1" if you pass it to printf("%d")), but it means that code like this: status |= sigc->trust_level < configured_min_trust_level; does not necessarily behave as expected. This turns out not to be a bug in practice, though, because we keep the "-1" only when gpg did not report a signature from a known key, in which case the line above: status |= sigc->result != 'G'; would always set status to non-zero anyway. So only a 'G' signature with no parsed trust level would cause a problem, which doesn't seem likely to trigger (outside of unexpected gpg behavior). 2. When using the "%GT" format placeholder, we pass the value to gpg_trust_level_to_str(), which complains that the value is out of range with a BUG(). This behavior was introduced by 803978d (gpg-interface: add function for converting trust level to string, 2022-07-11). Before that, we just did a switch() on the enum, and anything that wasn't matched would end up as the empty string. Curiously, solving this by naively doing: if (level < 0) return ""; in that function isn't sufficient. Because of (1) above, the compiler can (and does in my case) actually remove that conditional as dead code! We can solve both by representing this state as an enum value. We could do this by adding a new "unknown" value. But this really seems to match the existing "undefined" level well. GPG describes this as "Not enough information for calculation". We have tests in t7510 that trigger this case (verifying a signature from a key that we don't have, and then checking various %G placeholders), but they didn't notice the BUG() because we didn't look at %GT for that case! Let's make sure we check all %G placeholders for each case in the formatting tests. The interesting ones here are "show unknown signature with custom format" and "show lack of signature with custom format", both of which would BUG() before, and now turn %GT into "undefined". Prior to 803978d they would have turned it into the empty string, but I think saying "undefined" consistently is a reasonable outcome, and probably makes life easier for anyone parsing the output (and any such parser had to be ready to see "undefined" already). The other modified tests produce the same output before and after this patch, but now we're consistently checking both %G? and %GT in all of them. Signed-off-by: Jeff King <[email protected]> Reported-by: Rolf Eike Beer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 73876f4 commit 7891e46

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

gpg-interface.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ int check_signature(struct signature_check *sigc,
633633
int status;
634634

635635
sigc->result = 'N';
636-
sigc->trust_level = -1;
636+
sigc->trust_level = TRUST_UNDEFINED;
637637

638638
fmt = get_format_by_sig(signature);
639639
if (!fmt)

t/t7510-signed-commit.sh

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -221,84 +221,91 @@ test_expect_success GPG 'amending already signed commit' '
221221
test_expect_success GPG 'show good signature with custom format' '
222222
cat >expect <<-\EOF &&
223223
G
224+
ultimate
224225
13B6F51ECDDE430D
225226
C O Mitter <[email protected]>
226227
73D758744BE721698EC54E8713B6F51ECDDE430D
227228
73D758744BE721698EC54E8713B6F51ECDDE430D
228229
EOF
229-
git log -1 --format="%G?%n%GK%n%GS%n%GF%n%GP" sixth-signed >actual &&
230+
git log -1 --format="%G?%n%GT%n%GK%n%GS%n%GF%n%GP" sixth-signed >actual &&
230231
test_cmp expect actual
231232
'
232233

233234
test_expect_success GPG 'show bad signature with custom format' '
234235
cat >expect <<-\EOF &&
235236
B
237+
undefined
236238
13B6F51ECDDE430D
237239
C O Mitter <[email protected]>
238240
239241
240242
EOF
241-
git log -1 --format="%G?%n%GK%n%GS%n%GF%n%GP" $(cat forged1.commit) >actual &&
243+
git log -1 --format="%G?%n%GT%n%GK%n%GS%n%GF%n%GP" $(cat forged1.commit) >actual &&
242244
test_cmp expect actual
243245
'
244246

245247
test_expect_success GPG 'show untrusted signature with custom format' '
246248
cat >expect <<-\EOF &&
247249
U
250+
undefined
248251
65A0EEA02E30CAD7
249252
Eris Discordia <[email protected]>
250253
F8364A59E07FFE9F4D63005A65A0EEA02E30CAD7
251254
D4BE22311AD3131E5EDA29A461092E85B7227189
252255
EOF
253-
git log -1 --format="%G?%n%GK%n%GS%n%GF%n%GP" eighth-signed-alt >actual &&
256+
git log -1 --format="%G?%n%GT%n%GK%n%GS%n%GF%n%GP" eighth-signed-alt >actual &&
254257
test_cmp expect actual
255258
'
256259

257260
test_expect_success GPG 'show untrusted signature with undefined trust level' '
258261
cat >expect <<-\EOF &&
262+
U
259263
undefined
260264
65A0EEA02E30CAD7
261265
Eris Discordia <[email protected]>
262266
F8364A59E07FFE9F4D63005A65A0EEA02E30CAD7
263267
D4BE22311AD3131E5EDA29A461092E85B7227189
264268
EOF
265-
git log -1 --format="%GT%n%GK%n%GS%n%GF%n%GP" eighth-signed-alt >actual &&
269+
git log -1 --format="%G?%n%GT%n%GK%n%GS%n%GF%n%GP" eighth-signed-alt >actual &&
266270
test_cmp expect actual
267271
'
268272

269273
test_expect_success GPG 'show untrusted signature with ultimate trust level' '
270274
cat >expect <<-\EOF &&
275+
G
271276
ultimate
272277
13B6F51ECDDE430D
273278
C O Mitter <[email protected]>
274279
73D758744BE721698EC54E8713B6F51ECDDE430D
275280
73D758744BE721698EC54E8713B6F51ECDDE430D
276281
EOF
277-
git log -1 --format="%GT%n%GK%n%GS%n%GF%n%GP" sixth-signed >actual &&
282+
git log -1 --format="%G?%n%GT%n%GK%n%GS%n%GF%n%GP" sixth-signed >actual &&
278283
test_cmp expect actual
279284
'
280285

281286
test_expect_success GPG 'show unknown signature with custom format' '
282287
cat >expect <<-\EOF &&
283288
E
289+
undefined
284290
65A0EEA02E30CAD7
285291
286292
287293
288294
EOF
289-
GNUPGHOME="$GNUPGHOME_NOT_USED" git log -1 --format="%G?%n%GK%n%GS%n%GF%n%GP" eighth-signed-alt >actual &&
295+
GNUPGHOME="$GNUPGHOME_NOT_USED" git log -1 --format="%G?%n%GT%n%GK%n%GS%n%GF%n%GP" eighth-signed-alt >actual &&
290296
test_cmp expect actual
291297
'
292298

293299
test_expect_success GPG 'show lack of signature with custom format' '
294300
cat >expect <<-\EOF &&
295301
N
302+
undefined
296303
297304
298305
299306
300307
EOF
301-
git log -1 --format="%G?%n%GK%n%GS%n%GF%n%GP" seventh-unsigned >actual &&
308+
git log -1 --format="%G?%n%GT%n%GK%n%GS%n%GF%n%GP" seventh-unsigned >actual &&
302309
test_cmp expect actual
303310
'
304311

0 commit comments

Comments
 (0)