DEV Community

Cover image for Silent Failure #18: The Line That Said OK While the Command Exited 1
Renhua Wei
Renhua Wei

Posted on

Silent Failure #18: The Line That Said OK While the Command Exited 1

Silent Failure #18: The Line That Said OK While the Command Exited 1

My runbook step for TLS was one pipe long:

openssl s_client -connect $h:443 -servername $h -verify_return_error </dev/null | grep "Verify return code: 0"
Enter fullscreen mode Exit fullscreen mode

Green on every hostname I own, for months. Then I ran it against a host whose certificate only covered a sibling name — a cert I had deliberately asked for, precisely to see this line go red — and it printed Verify return code: 0 (ok) and exited 0 with a grep count of 1. Green on a broken cert. I sat there re-reading the hostname in the connect string as if the typo could be mine rather than the tool's.

The typo was mine, in a way. Two typos, one on top of the other.

The first one was the pipe. cmd | grep returns grep's status, so the exit code of the actual handshake never reached the script. I knew this. I have written about this. I wrote it into a different runbook last month and then left this one alone because it was "obviously working". It was working in the sense that a smoke detector with the battery out also mounts neatly on the ceiling. It passed for the wrong reason, which is my least favourite kind of pass.

So I fixed it the way I should have the first time: drop the pipe, read $?. And the wrong-cert host still came back green. rc=0, no stderr, no complaint. That is the part that cost me the morning, because at that point I had done the thing I always tell people to do — gate on the exit status — and the exit status was lying too.

Here is what 3.5.7 actually does, all four runs from this morning:

$ openssl s_client -connect wrong.host.badssl.com:443 -servername wrong.host.badssl.com \
    -verify_return_error </dev/null
    Verify return code: 0 (ok)
$ echo $?
0

$ openssl s_client -connect wrong.host.badssl.com:443 -servername wrong.host.badssl.com \
    -verify_hostname wrong.host.badssl.com -verify_return_error </dev/null
verify error:num=62:hostname mismatch
    Verify return code: 0 (ok)
$ echo $?
1
Enter fullscreen mode Exit fullscreen mode

Same cert, same handshake, one flag apart: 0 then 1, and the summary line says 0 (ok) in both. Expired and self-signed go the other way — rc=1, with num=10:certificate has expired and num=18:self-signed certificate on stderr, and the stdout summary still printing 0 (ok) as the process walks out the door.

So there are two separate results in that output and I had been reading the wrong one — I assumed the summary line was cosmetic noise and the exit status was the verdict, and that assumption is what put the bad line in the runbook in the first place. -verify_return_error makes chain errors abort the handshake, which is why expired and self-signed give you rc=1. Hostname checking is not part of the chain — it only runs if you hand s_client a name to check with -verify_hostname — so a mismatched name sails through the handshake with a perfectly valid chain and exits 0. And the Verify return code line is printed from the chain result, which is genuinely 0 in every one of those cases. It is not stale, it is not a bug, it just answers a narrower question than the one I was asking. A status line that is correct about the wrong thing is worse than no line at all, because it gives the next person something to grep.

I was wrong twice on the same runbook, in the same direction, and the second time I was wrong with the fix I had recommended to other people.

Then the third one, which is the funny one. While chasing this I wanted to see the chain the server actually served, so I built a loop with curl, and because some of our internal hosts use a private CA I lazily threw -k into the loop while I sorted the CA file out. Every host came back with error 20, including the good ones. I spent a while convinced a stale intermediate was being served, which is a real thing that happens, and I chased it properly before I did the boring thing and opened the source.

curl 7.88.1, lib/vtls/openssl.c: populate_x509_store() opens if(verifypeer) { at line 3071 and does not close until line 3278. The CA file load — X509_STORE_load_file(store, ssl_cafile) — is at 3230, inside that block. So is the --capath load, so is the CURL_CA_FALLBACK default-paths branch. With -k, verifypeer is 0 and the whole block is skipped: no CA file, no CA path, no default store. An empty store reports every normal chain as 20, which is "unable to get local issuer certificate" — a message that is true and tells you nothing. Self-signed stays at 18 because that check fires before the store is ever consulted.

--cacert on the command line, silently ignored, because I asked for insecure. Fine. My own doing, and the loop was measuring nothing but the emptiness I had engineered into it.

What I run now is three lines and one canary:

check_tls() {
  openssl s_client -connect "$1:443" -servername "$1" -verify_hostname "$1" \
    -verify_return_error </dev/null >/tmp/sc.out 2>/tmp/sc.err
  rc=$?
  [ "$rc" -eq 0 ] || { echo "TLS FAIL $1 rc=$rc $(grep -m1 'verify error' /tmp/sc.err)"; return 1; }
}
# the canary: must fail, or you are not testing anything
check_tls wrong.host.badssl.com && { echo "gate is dead"; exit 1; }
Enter fullscreen mode Exit fullscreen mode

No pipe, no grep on the verdict, -verify_hostname always, and a known-bad host that has to go red before I trust a green on a real one. For curl I keep the exit code for the yes/no and read %{ssl_verify_result} when I need the reason, because 60 is what curl returns for expired, wrong-name and untrusted alike — one exit code, three different worlds.

What I'd test first

Feed your own gate a cert that is wrong in exactly one way, and make it prove the failure. Wrong name only, expired only, self-signed only, and check the exit code on each — not the human-readable line. If a single one of them comes back 0, the gate is the next thing you fix, because everything downstream of it is now advisory. The cheap version of this is the canary above: a hostname mismatch is the case that slips through, since the chain is valid and the only thing wrong is the name you asked for.

My take, and I'll take the argument: exit code or it didn't happen, and any tool that prints a human-readable verdict next to a machine-readable one has already told you which one the runbook will grep. Verify return code: 0 (ok) sitting on stdout while the process exits 1 is a footgun shaped like documentation, and the fact that it stayed correct for years just means the bug was deferred, not absent.

I've only measured this on OpenSSL 3.5.7 against the badssl set and curl 7.88.1, so my sample is one machine and one afternoon.

On 3.x, does your gate still trust -verify_return_error alone for hostname, or did you wire -verify_hostname in after a mismatch walked through? I want the case where the flag that saved you was something nobody puts in the runbook.

Top comments (0)