I will admit that people consider it a little surprising and non-hackers will be very loudly booed if they rely on this behavior but ed finds it way into lots of utilities like this. It's a remnant of a bygone era where people wanted their tools to have this kind of power.
But the author is absolutely right, nowadays we should prefer red. Still a small amount of shame if you're applying untrusted patches.
The early computing era (I'd say until early '90s) was definitely had something special in the way so much relied on trust, both trusting the user and trusting the wider community. Today everything is so locked down, paranoid and anxiety inducing that computing has become increasingly stressful and unfun.
I'm also reminded by a RMS writing (I think it was letter of some sort) campaigning that denying computer lab users root access was oppressive. Bit sad that I can't actually find it now.
And of course the classic (if misused like I'm doing here) Franklin quote "Those who would give up essential Liberty, to purchase a little temporary Safety, deserve neither Liberty nor Safety."
I don't think it's so much that people wanted patch to be able to run arbitrary code, but rather that writing your tools to be very flexible allows you to get the functionality you need with very little code. The first version of ctags was written as a shell script that invoked ed to do the heavy lifting, for example. It was a small fraction of the rewrite of ctags in C in later Unixes.
So that's how you end up with m4 being Turing-complete, being able to put shell commands in anything, that sort of thing.
>It's a remnant of a bygone era where people wanted their tools to have this kind of power.
You should still want your tools to have this power.
It's amazing what you can do with tools built like this and how _easy_ it is. People use the same motivation for making microservices, "do one thing well", they just don't think about it for command line utilities and treat shell scripting like a second class citizen.
This is bad and should be fixed, but there are fairly few circumstances where it actually creates a new vulnerability. The majority of uses of patch are applied to source code by someone who's going to end up running that code anyways, so applying patches you haven't read closely from sources you don't trust is already unsafe.
> The majority of uses of patch are applied to source code by someone who's going to end up running that code anyways, so applying patches you haven't read closely from sources you don't trust is already unsafe.
I think you are overlooking a case that I suspect is common: applying a patch without reading the patch source and then using your source code control system to review what the patch did.
That lets you review that patch using whatever tools you normally use for reviewing code changes, which are often much nicer than reading a raw ed script.
The main place where this would be an issue would be if you have a server that is not going to build or run code, but applies patches to source and outputs the result.
Patch applies files created with diff. Diff has 4 different output formats. One of those formats is basically a batch script with ed instructions.
For patch to work correctly you have to allow executing ed commands (internally or by spawning ed) but for security reasons you better not let ed execute yet another program.
Off topic, and forgive my naivety & ignorance, but I really do not understand the highly specific nature of this Principle / Law, as it is on wiki:
"Every program attempts to expand until it can read mail. Those programs which cannot so expand are replaced by ones which can."
... was Mail just the example at the time? and this is basically just the a generic reference to feature/scope creep?... I just don't get the highly specific inclusion of "Reading mail" as something that all programs expand towards.
Does anyone know if the GNU tools, the coreutils, have been through security audits and fuzzing? They’re the most used tools on the planet, I’d say, and relying on the 90’s “many eyes make all bugs shallow” doesn’t seem to cut it anymore...
FreeBSD's patch runs red, a change we picked up from DragonFly BSD. That said OpenBSD's change to internally handle ed-style diffs is the best approach and I expect FreeBSD will migrate to that.
similarly, i recall once being denied root on a host while trying to help out with something and then asking if i could at least have sudo less to look at things... which was granted. >:D (then i fixed the problem)
"patch will attempt to determine the type of the diff listing, unless over-ruled by a -c, -e, -n, or -u option.Context diffs (old-style, new- style, and unified) and normal diffs are applied directly by the patch program itself, whereas ed diffs are simply fed to the ed(1) editor via a pipe."
According to this, context diffs are not sent to ed.
Is the author suggesting that patch can be fooled to interpret a context diff as an ed diff?
Theres a file called pch.c with an excessive amount of parsing and "intuit" functions like intuit_diff_type().
patch has anthromorphised progress and error messages and tries to "guess".
However I am only a dumb end user. I should not question what I do not understand. Its all safe I'm sure.
The `patch` program is expected to be able to apply any patchfile syntaxes created by `diff`. Once upon a time, the default behavior of diff was to spit out an `ed` script (nowadays, it's behind the `-e` flag). So, to apply that syntax of patchfile, it invokes `ed`.
This is, of course, a vulnerability. But let's not get too crazy. Once you are applying a patch, either you trust the patch or not.
If you blindly apply patches, you will be in risk as soon as you run or try to compile the patched command. This attack is just a bit faster because it happens as soon as you apply the patch.
[+] [-] Spivak|8 years ago|reply
But the author is absolutely right, nowadays we should prefer red. Still a small amount of shame if you're applying untrusted patches.
[+] [-] zokier|8 years ago|reply
I'm also reminded by a RMS writing (I think it was letter of some sort) campaigning that denying computer lab users root access was oppressive. Bit sad that I can't actually find it now.
And of course the classic (if misused like I'm doing here) Franklin quote "Those who would give up essential Liberty, to purchase a little temporary Safety, deserve neither Liberty nor Safety."
[+] [-] kragen|8 years ago|reply
So that's how you end up with m4 being Turing-complete, being able to put shell commands in anything, that sort of thing.
[+] [-] colechristensen|8 years ago|reply
You should still want your tools to have this power.
It's amazing what you can do with tools built like this and how _easy_ it is. People use the same motivation for making microservices, "do one thing well", they just don't think about it for command line utilities and treat shell scripting like a second class citizen.
[+] [-] gbrown_|8 years ago|reply
https://marc.info/?l=openbsd-cvs&m=144498099601083&w=2
[+] [-] seanhunter|8 years ago|reply
In case anyone has not been exposed to the reference, it's perhaps invoking the famous `ed, man !man ed` page. https://www.gnu.org/fun/jokes/ed-msg.html
[+] [-] rachelbythebay|8 years ago|reply
[+] [-] knodi123|8 years ago|reply
[+] [-] jimrandomh|8 years ago|reply
[+] [-] tzs|8 years ago|reply
I think you are overlooking a case that I suspect is common: applying a patch without reading the patch source and then using your source code control system to review what the patch did.
That lets you review that patch using whatever tools you normally use for reviewing code changes, which are often much nicer than reading a raw ed script.
[+] [-] archgoon|8 years ago|reply
So a cr review website could be affected.
[+] [-] tomc1985|8 years ago|reply
[+] [-] IshKebab|8 years ago|reply
[+] [-] simlevesque|8 years ago|reply
[+] [-] secure|8 years ago|reply
% patch<evil.patch sh: 1: ed: not found patch: ed FAILED
patch works just fine for me, though, so ed is not required.
[+] [-] ben_bai|8 years ago|reply
For patch to work correctly you have to allow executing ed commands (internally or by spawning ed) but for security reasons you better not let ed execute yet another program.
[+] [-] cbr|8 years ago|reply
(I don't think I've ever used a system that didn't have ed installed by default.)
[+] [-] tantalor|8 years ago|reply
https://en.wikipedia.org/wiki/Zawinski%27s_Law
[+] [-] dfsegoat|8 years ago|reply
"Every program attempts to expand until it can read mail. Those programs which cannot so expand are replaced by ones which can."
... was Mail just the example at the time? and this is basically just the a generic reference to feature/scope creep?... I just don't get the highly specific inclusion of "Reading mail" as something that all programs expand towards.
[+] [-] Blackthorn|8 years ago|reply
Amazing how an ancient vuln can still be found hidden in plain sight.
[+] [-] oblio|8 years ago|reply
[+] [-] emaste|8 years ago|reply
[+] [-] volkadav|8 years ago|reply
[+] [-] jacquesm|8 years ago|reply
[+] [-] aplorbust|8 years ago|reply
According to this, context diffs are not sent to ed.
Is the author suggesting that patch can be fooled to interpret a context diff as an ed diff?
Theres a file called pch.c with an excessive amount of parsing and "intuit" functions like intuit_diff_type().
patch has anthromorphised progress and error messages and tries to "guess".
However I am only a dumb end user. I should not question what I do not understand. Its all safe I'm sure.
[+] [-] gtrubetskoy|8 years ago|reply
[+] [-] gh02t|8 years ago|reply
[+] [-] Jasper_|8 years ago|reply
[+] [-] jwilk|8 years ago|reply
[+] [-] teeray|8 years ago|reply
[+] [-] eridius|8 years ago|reply
[+] [-] LukeShu|8 years ago|reply
[+] [-] schoen|8 years ago|reply
https://en.wikipedia.org/wiki/Diff_utility#Edit_script
[+] [-] zokier|8 years ago|reply
[+] [-] brynet|8 years ago|reply
[+] [-] hannob|8 years ago|reply
[+] [-] zitterbewegung|8 years ago|reply
[+] [-] rachelbythebay|8 years ago|reply
[+] [-] gruez|8 years ago|reply
[+] [-] paulie_a|8 years ago|reply
[+] [-] gaius|8 years ago|reply
[+] [-] unknown|8 years ago|reply
[deleted]
[+] [-] yiyus|8 years ago|reply
If you blindly apply patches, you will be in risk as soon as you run or try to compile the patched command. This attack is just a bit faster because it happens as soon as you apply the patch.
[+] [-] mrob|8 years ago|reply
[+] [-] schoen|8 years ago|reply
[+] [-] 0x0|8 years ago|reply
[+] [-] cs702|8 years ago|reply
[+] [-] vilhelm_s|8 years ago|reply