10 ms·
Best of show – abuse of libc
- dang 6y agoGeneral thread here: https://news.ycombinator.com/item?id=25651942 https://news.ycombinator.com/item?id=25651942
- SftwreEngnr 6y agoThanks for nothing, dang!
- navaati 6y agoThanks for all, dang !
- aftbit 6y agoPersonally I find it amusing that the 0-signal comment "Thanks for all dang" is upvoted while the opposite 0-signal comment "Thanks for nothing dang" is downvoted. I mean, I think dang is chill, but neither of these really contributes to the discussion any more than the other, so shouldn't they have the same score? Upvotes really are a popularity contest these days.
- saagarjha 6y agoIt's not clear what the votes are. However, the latter was written by someone who has banned for what appears to be their habit of leaving low-value comments.
- michaelcampbell 6y agoAlways have been.
- dang 6y agoThis was addressed by pg over a decade ago: https://news.ycombinator.com/newswelcome.html https://news.ycombinator.com/newswelcome.html Empty comments can be ok if they're positive. There's nothing wrong with submitting a comment saying just "Thanks." What we especially discourage are comments that are empty and negative—comments that are mere name-calling. If you think in terms of what's good/bad for community it may make more sense. (I hope it's clear this applies whether or not the mods were mentioned in either a positive or negative way.)
- thedufer 6y agoThe negative comment is from a hellbanned account (which is, frankly, unsurprising). It's not even possible to downvote it, as far as I can tell, due to being DOA.
- deleted 6y ago[deleted]
- kderbyma 6y agoawesome. I didn't know about that printf hack....time for some fun experiments
- saagarjha 6y agoBe careful, though, you don't want anyone to hack you through printf ;)
- segfaultbuserr 6y agoIn the late 90s, looking for "printf(string)" [0] in the code was a great way to discover remote code execution 0days ;-) [0] should be "printf("%s", string)".
- stevekemp 6y agoVery much so, it took a long time for this to become obvious as a security problem. My memory wuftpd was the first big program to suffer from this class of attacks.
- segfaultbuserr 6y agoIt reminds me of a talk by infosec researcher "The Grugq" about opsec techniques used by blackhat hackers. Its subtitle was because jail is only for wuftpd, I couldn't stop laughing at it.
- Groxx 6y ago>Format specifiers can take extra “arguments”. - "%hhn": store the number of bytes written mod 256 to the char pointer ... Oh boy. I'll put that down for my "thing I don't think I wanted to know" of the day.
- rightbyte 6y agoThere is some innocent beauty in the twistedness of printf - especially with GNU extensions.
- tomjakubowski 6y agoGNU's printf specifier language is Turing complete, I believe.
- alisonkisk 6y agoTheres a great example of what you can do with this, submitted and discussed on HN here: https://news.ycombinator.com/item?id=25690319 https://news.ycombinator.com/item?id=25690319
- notretarded 6y ago>Format specifiers can take extra “arguments”. - "%hhn": store the number of bytes written mod 256 to the char pointer ... Oh boy. I'll put that down for my "thing I don't think I wanted to know" of the day.
- badsectoracula 6y agoUp next: a C compiler that compiles to printf statements :-P
- hahajk 6y agohttps://github.com/HexHive/printbf https://github.com/HexHive/printbf well this is a brainfuck interpreter inside printf. I’m pretty sure there are plenty of c-to-bf transpilers.
- felixr 6y agoThis is by the same author as the ioccc entry and also one of authors of the paper showing the turing completeness of printf http://nebelwelt.net/publications/#15SEC http://nebelwelt.net/publications/#15SEC
- lifthrasiir 6y agoAh, that explains everything. I have already seen this technique before and wondered why this entry has to be the best of show---I don't doubt it is worth the prize, just that it didn't sound very novel. But it all makes sense if the technique is not well known and authors tried to revitalize that.
- klyrs 6y agoThat's fun, but esoteric languages in general and brainfuck in specific tend to lack things you'd want out of c: file system access, system calls, etc.
- archi42 6y agoHm, I think you could add numeric syscalls, similar to what happens at the asm level. E.g. put the syscall id and some parameters on the "stack", then let the interpreter run the syscall with a new "instruction" e.g. '!'. This could even substitute '.' (putchar) and ',' (getchar), since these are very much just syscalls. So that would reduce the number of instructions by one (to 7). Oh, getting to 6 would also be fun: One might replace '[' and ']' with a conditional branch '?'. It just needs two parameters: condition and (signed) number of instructions to jump. Adds the bonus (much like normal asm) to write moch more ~~horribly abusive~~ flexible control flow than a structured "while(*ptr)".
- lxe 6y agoHow did printf end up here in the first place? Decades of feature additions, or were these features a part of an early spec?
- segfaultbuserr 6y agoFun fact, on glibc, an extension feature is that you can define your own custom conversion specifiers for printf().
- wahern 6y ago%n was defined in C89, the first C standard: http://port70.net/~nsz/c/c89/c89-draft.html#4.9.6.1 http://port70.net/~nsz/c/c89/c89-draft.html#4.9.6.1 Looking at old source code, the earliest implementation I found is 4.3BSD Tahoe (1988). See https://www.tuhs.org/cgi-bin/utree.pl?file=4.3BSD-Tahoe/usr/src/lib/libc/stdio/doprnt.c https://www.tuhs.org/cgi-bin/utree.pl?file=4.3BSD-Tahoe/usr/... Second oldest I found was Tenth Edition [Research] Unix (1989). See ocvt_n at https://www.tuhs.org/cgi-bin/utree.pl?file=V10/libc/stdio/vfprintf.c https://www.tuhs.org/cgi-bin/utree.pl?file=V10/libc/stdio/vf... I couldn't find support in earlier implementations archived on that site.
- segfaultbuserr 6y ago> Looking at old source code, the earliest implementation I found is 4.3BSD Tahoe (1988). You are the HN historian of the day.
- wahern 6y agoAnother interesting factoid is that macOS only supports %n if the format string is located in read-only memory. Per printf(3) on macOS: > For this reason, a format argument containing %n is assumed to be untrustworthy if located in writable memory (i.e. memory with protection PROT_WRITE; see mprotect(2)) and any attempt to use such an argument is fatal. Practically, this means that %n is permitted in literal format strings but disallowed in format strings located in normal stack- or heap-allocated memory. The manual page seems correct: % cat test.c #include <stdio.h> int main(void) { printf((char[]){ "%n" }, &(int){ 0 }); return 0; } % cc -o test test.c % ./test zsh: abort ./test
- saagarjha 6y agoFor those interested in more Turing complete format strings, look no further than the "sprint" challenge from this year's Google CTF Quals: https://ctftime.org/task/12834 https://ctftime.org/task/12834. It's sprintf in a loop this time and the program simulates a maze: https://github.com/google/google-ctf/tree/master/2020/quals/reversing-sprint https://github.com/google/google-ctf/tree/master/2020/quals/...
- enedil 6y agoThe author works at Google, so I suspect he's the same who created this challenge. Really enjoyable, although I didn't manage to solve it during contest.
- olliej 6y ago%n has frequently been used as an attack vector - generally in the context of the other poor practice of printf(<attacker controlled string>, ...) It’s actually intentionally disallowed in some libc implementations.
- rramadass 6y agoThe printf format string is actually a little language. In the book The Practice of Programming, Kernighan and Pike show how you can devise a similar format string to pack/unpack network packets.
- acekingspade 6y agoIt's hard to believe that this is the same person with multiple widely-cited ML papers[0]. It's jaw-dropping how talented someone can be. https://scholar.google.com/citations?user=q4qDvAoAAAAJ&hl=en&oi=ao https://scholar.google.com/citations?user=q4qDvAoAAAAJ&hl=en...
- onurgu 6y agoThank you for the pointer, it is really amazing.
- blue-dragonfly 6y agoA winner from 1993 is very interesting too: https://www.ioccc.org/years.html#1993_dgibson https://www.ioccc.org/years.html#1993_dgibson It implements Conway's Game of Life by creating a DSL using the C preprocessor and printf. The output is a program (several initial boards are supplied to bootstrap) which is the input program to be compiled and run to create the next generation. This is the program for a second generation: LIFE L _ _ _ _ _ L _ _ O _ _ L _ _ _ O _ L _ O O O _ L _ _ _ _ _ GEN 2 STAT 328960 END Each symbol like "LIFE" is a macro, the board is the program.
- deleted 6y ago[deleted]
- yakubin 6y agoI'm writing my first C99 compiler. IOCCC sounds like a great source of test material.