Fixing a false positive in a security guard is how you open a hole in it
Our guard flagged this command as a Keychain read: ps aux | grep -E "falcon|security" It is a process search. Nothing in it touches the Keychain. But the guard looks at each command the agent runs and splits it
Our guard flagged this command as a Keychain read:
ps aux | grep -E "falcon|security"
It is a process search. Nothing in it touches the Keychain. But the guard looks at each command the agent runs and splits it into parts, and the splitter drops quotes. After the split, the | inside the quoted pattern separates the command into pieces, and one piece is just the word security.
That matters because the macOS security program, run with no subcommand or with -i, reads its commands from stdin. There is no find-generic-password word to match, so the rule treats a bare security as dangerous. A piece that is only that word looks exactly like it.
The obvious fix is to skip the rule when the word sits inside a grep pattern. That is also the fix that goes wrong, and an independent reviewer showed us how over four rounds. Each time we loosened the check a little, they came back with a command that got through:
-
echo 'x|security' | cut -d'|' -f2 | xargs -I{} {}prints the word and then runs it. -
ls /usr/bin | grep -oE "x|security" | sh:grep -oprints only the matched text, so the word comes out the other side as a command. -
grep() { eval "$@"; }; grep "a|security"defines a function named grep, so "grep" proves nothing. -
ps aux | grep -oE "x|security" | sort -o run-mewrites a file, andsort --compress-program=shruns a program. - Where grep and cat are installed as ugrep and bat, long options like
--filterand--pagerrun a program too.
So the exemption we shipped is narrow. The word is ignored only when every stage of the whole pipeline starts with a program from a short list of read-and-print tools (ps, pgrep, grep, egrep, fgrep, rg, head, tail, wc, cut, tr, column, cat, ls, cd), by bare name, with nothing outside quotes but names, options, paths and |, &, ;. No $ or backtick inside double quotes, and no long option on any stage. This is the check that walks the quotes:
function plainListingText(text) {
let q = '';
for (let i = 0; i < text.length; i += 1) {
const c = text[i];
if (q === "'") { if (c === "'") q = ''; continue; }
if (q === '"') {
if (c === '\\') i += 1;
else if (c === '"') q = '';
else if (c === '$' || c === '`') return false;
continue;
}
if (c === "'" || c === '"') { q = c; continue; }
if (!PLAIN_UNQUOTED_RE.test(c)) return false;
}
return q === '';
}
Some harmless commands still get flagged: sudo grep, awk, grep --color and git log --grep "audit|security" all keep the old answer. We chose that on purpose. A false positive stops or questions a harmless command. A false negative on a credential rule is the thing the guard exists to prevent.
The test file lists the allowed commands and, in a much longer list, every bypass the reviewer found as a command that must still be denied. The denied list is the real specification. Every subcommand match, like find-generic-password and dump-keychain, is unchanged.
What to check on your own guard or allowlist:
- When you add an exception to a security rule, write the commands that must still be blocked before you write the exception. Make the exception pass the first list and fail the second.
- Ask what happens to text your exception lets through. Printing a word is harmless until a later stage runs it. Look at every stage after the match, not only the one that matched.
- Don't trust a program by name. A function, an alias, a path or a different build installed under that name can behave differently.
- Have someone who didn't write the exception try to get around it. We needed four rounds.
The guard is open source: https://github.com/IgorGanapolsky/mac-yolo-safeguards
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.