News for hackers. Stuff that matters.
Re:Glibc getaddrinfo stack-based buffer overflow by Munksgaard on Tuesday February 16, 2016 @09:51AM (#11110145)
Can we agree that it's urgently necessary to rewrite most of the core Linux/OSS stack in memory safe languages? Exploits like this come up all the time, and we know how to completely eliminate them. I don't care if it's Rust or D or Go or Haskell or OCaml or anything else, as long as it's not C. The sooner we do this, the better.
Re:Glibc getaddrinfo stack-based buffer overflow by laumars on Tuesday February 16, 2016 @10:07AM (#11110255)
"memory safe" languages aren't a silver bullet and urgently rewriting core libraries in a new language can be more dangerous than our current process of analyzing and fixing the existing vulnerabilities in our current libraries.

However I'm all for experimental research projects developing alternative solutions.

Re:Glibc getaddrinfo stack-based buffer overflow by marcosdumay on Tuesday February 16, 2016 @11:16AM (#11110836)
Of course, there's no silver bullet.

But this is yet another bug that could not exist at all in a language with automatic memory management. And there are lots and lots of those bugs.

Re:Glibc getaddrinfo stack-based buffer overflow by laumars on Tuesday February 16, 2016 @12:42PM (#11111629)
Indeed. But my point was there's lots and lots of ways code can have bugs. Vulnerabilities like these are serious, but rushing through a reimplementation in another language is bound to fail in a multitude of other ways.

What we need is not to have knee-jerk reactions akin to angry mops carrying flaming torches and sharpened pitchforks. What we need is to praise the excellent work researchers are doing and to offer our time and/or money into:

a) more security research,

b) development of experimental alternatives written in memory safe languages,

c) yet more security research except this time on the experimental counterparts,

d) thorough testing of the experimental counterparts to ensure functional compatibility.

Maybe a few years down the line we will have something usable in a few bleeding edge Linux distributions, but it will be even longer still before it will have the backing of Debian, RHEL and SLES.

Re:Glibc getaddrinfo stack-based buffer overflow by pjmlp on Tuesday February 16, 2016 @10:38AM (#11110511)
Apparently until the UNIX culture that worships C exist, it won't happen.

C only became a thing in computing after UNIX workstations became popular.

Before it, there were quite a few safer systems programming languages to choose from, that got steam rolled by C adoption in OS SDKs.

Due to its relation with UNIX, it will most likely happen with Apple (Swift), Microsoft (.NET Native, System C#, C++/GSL), Google (Java FTW on Android, Go) than any other OS vendor.

But I am also looking forward any of them pick D, Rust, <whatever safe lang> in the process.

Re:Glibc getaddrinfo stack-based buffer overflow by IgorPartola on Tuesday February 16, 2016 @11:07AM (#11110753)
> Java FTW on Android

This made me smile. Need I point out that Android runs on top of Linux? Or that you can still use C/C++ on Android? http://developer.android.com/tools/sdk/ndk/index.html

Re:Glibc getaddrinfo stack-based buffer overflow by pjmlp on Tuesday February 16, 2016 @11:27AM (#11110934)
Yes and I use it.

I also feel the pains that Android teams imposes on the NDK users to use it as little as possible.

If you bothered to read the contents of the link that you pushed.

"Notably, using native code on Android generally does not result in a noticable performance improvement, but it always increases your app complexity. In general, you should only use the NDK if it is essential to your app—never because you simply prefer to program in C/C++. When examining whether or not you should develop in native code, think about your requirements and see if the Android framework APIs provide the functionality that you need"

Also Android Java is nowadays AOT compiled to native code. So whatever C and C++ dependencies there are, could certainly eventually be replaced if there were willingness to do so.

Besides, have you ever tried to implement libc in ANSI C, without language extensions and a sprinkle of Assembly?

Re:Glibc getaddrinfo stack-based buffer overflow by IgorPartola on Tuesday February 16, 2016 @11:37AM (#11111021)
Nonetheless, it's something that is available to you as a developer. I am firmly in the "right tool for the job" camp, and I can see situations where C is the better tool. I don't see why mobile app development would benefit from it, but I almost never have the opportunity to work with Android.

I have never attempted to put together a full libc in ANSI C, no. My experience there is limited to putting together parts of it for various microcontrollers, where I was more or less bound to ANSI C and chose to use no assembly.

That's a long way of saying, what's your point? And why do you feel the need to get personal?

Re:Glibc getaddrinfo stack-based buffer overflow by pjmlp on Tuesday February 16, 2016 @11:53AM (#11111188)
Excuse me if it felt personal it wasn't my intention, just the way I write.

Non native speaker here.

As I mentioned in the reply and in another thread, I do use the NDK, but I wouldn't if there were better alternatives.

30 years ago those microcontrollers would probably be programmable in Assembly and Forth, Pascal, Basic and C dialects. At least when I think about Z80 like ones.

Re:Glibc getaddrinfo stack-based buffer overflow by lmm on Tuesday February 16, 2016 @11:53AM (#11111184)
Just because it's not perfect doesn't mean it's not better. Yes Android runs x000 lines of C and that's x000 lines too many, but it's still a substantial improvement over desktop systems.
Re:Glibc getaddrinfo stack-based buffer overflow by tosseraccount on Tuesday February 16, 2016 @11:29AM (#11110946)
... and apple runs of top of BSD.

C popularity pre-dates workstations. DEC (Digital Equipment Corporation ) , maker of popular mini computers, used it extensively.

Re:Glibc getaddrinfo stack-based buffer overflow by pjmlp on Tuesday February 16, 2016 @11:37AM (#11111035)
I only bothered to use C when forced into UNIX world via Xenix.

Yes Apple runs on top of BSD, because they weren't able to write a new OS and had to buy one, which happened to be built on top of BSD.

Once upon a time they had their OS written in Object Pascal, but then they decided to rewrite it in C to welcome those UNIX devs.

Nothing prevents them to rewrite parts of the OS in Swift after the language gets more mature.

It is only a question of willingness, political will and money.

Re:Glibc getaddrinfo stack-based buffer overflow by mikeash on Tuesday February 16, 2016 @12:52PM (#11111729)
What Apple OS was written in Object Pascal? It looks like they had an app framework written in that language, but the OS never was. I believe the Mac OS at the time was predominantly written in assembly, possibly with some (non-object) Pascal parts.
Re:Glibc getaddrinfo stack-based buffer overflow by pjmlp on Tuesday February 16, 2016 @01:55PM (#11112214)
Mac OS, the initial versions.

It was a mix of Object Pascal and Assembly. The Pascal extensions that Borland later adopted in TP 5.5.

Re:Glibc getaddrinfo stack-based buffer overflow by mikeash on Tuesday February 16, 2016 @02:00PM (#11112248)
The initial versions were pure assembly. There was no room for any high-level code. As time went on and more storage was available, that became a possibility, but much of the OS remained assembly. This was a big part of why so much of the OS ran under emulation when the PowerPC Macs were introduced.
Re:Glibc getaddrinfo stack-based buffer overflow by pjmlp on Tuesday February 16, 2016 @03:52PM (#11113039)
Well here there is a bit of different story being told.

http://www.folklore.org/

Search for Pascal.

Cherry picked from the search results

http://www.folklore.org/StoryView.py?project=Macintosh&story...

The fact that many C programmers forget, is that it is impossible to implement the C runtime or an OS with pure ANSI C code.

Somehow it is ok for C to use Assembly and compiler specific extensions, but not for other systems programming languages to do the same.

Re:Glibc getaddrinfo stack-based buffer overflow by mikeash on Tuesday February 16, 2016 @03:57PM (#11113077)
Nothing in that link says that the OS was written in Pascal. Yes, Pascal was a popular language for Mac apps, but of course nothing says that the OS has to be written in the same language used for writing programs that run on it.

I don't understand your last comment. I am not saying that the OS doesn't qualify as "written in Pascal" because it contains assembly. I am saying it doesn't qualify because it didn't contain Pascal. The early versions were subject to absurd space constraints that required everything to be written in assembly. My understanding is that high level languages started to be used for parts of the OS later on, but it remained predominantly assembly until well after the PowerPC transition.

Re:Glibc getaddrinfo stack-based buffer overflow by pjmlp on Tuesday February 16, 2016 @05:09PM (#11113511)
Well, I was kind of right and you as well.

It was the Lisa that was written in Pascal.

http://www.folklore.org/StoryView.py?project=Macintosh&story...

Re:Glibc getaddrinfo stack-based buffer overflow by mikeash on Tuesday February 16, 2016 @12:48PM (#11111691)
I wouldn't say that UNIX workstations became truly popular until after Mac OS X took off sometime in the 2000s. C and C++ were enormously popular in the 90s, if not earlier. They were really common for Mac and Windows programming, and I don't think that had anything to do with the UNIX connection.
Re:Glibc getaddrinfo stack-based buffer overflow by pjmlp on Tuesday February 16, 2016 @01:52PM (#11112191)
I was there.

The only reason to get a C compiler for CP/M, MS-DOS or Amiga was to be able to bring work home from those expensive workstations at work or university.

Re:Glibc getaddrinfo stack-based buffer overflow by mikeash on Tuesday February 16, 2016 @02:02PM (#11112266)
CP/M, MS-DOS, and Amiga sound like the 80s, not the 90s. I recall that C was not super popular at that time (I arbitrarily chose to start learning Pascal around 1988, given a choice between C and Pascal, probably the wrong choice in hindsight) but by the early/mid 90s it was pretty huge.
Re:Glibc getaddrinfo stack-based buffer overflow by marshray on Tuesday February 16, 2016 @03:23PM (#11112865)
I was there too. C was a common language for developing from-scratch DOS applications in the 1980's. Sometimes portability was a consideration, often not. Mark Williams and Microsoft C were two popular compilers.
Re:Glibc getaddrinfo stack-based buffer overflow by pjmlp on Tuesday February 16, 2016 @03:45PM (#11113006)
Microsoft C before Visual C++ being popular?!

Not on my country, it was all about Borland compilers, even in the Windows 3.x days.

Microsoft was even the last vendor to add support for C++ on the last MS-DOS version of Microsoft C, version 7 if I remember correctly.

Re:Glibc getaddrinfo stack-based buffer overflow by marshray on Tuesday February 16, 2016 @03:58PM (#11113079)
Yes, Borland "Turbo" C was another massively popular choice.

But you can't simultaneously argue that C wasn't used for PC platform development in the 1980's and that Borland C was dominant.

Re:Glibc getaddrinfo stack-based buffer overflow by pjmlp on Tuesday February 16, 2016 @05:05PM (#11113492)
> it was all about Borland compilers

I never said directly I was referring to Borland C, actually it was all about Turbo Basic and Turbo Pascal.

I and a few others did use Turbo C, but we jumped right into Turbo C++ the year thereafter, as we could not envision using plain C after having done systems programming in the other Borland languages.

C was left for programming classes, usually trying to convince the teacher to use C++ instead for projects, sadly it only worked occasionally.

Re:Glibc getaddrinfo stack-based buffer overflow by hendzen on Tuesday February 16, 2016 @09:57AM (#11110182)
So are you personally volunteering to rewrite all of Linux and glibc in Rust? Are you or your employer volunteering to fund such efforts? There is a reason it hasn't happened yet - because doing so would be exceptionally time consuming and expensive.
Re:Glibc getaddrinfo stack-based buffer overflow by efuquen on Tuesday February 16, 2016 @10:06AM (#11110249)
I think OP isn't naively assuming it would be easy, or any one person would do it. More the point is there probably isn't enough of an effort or emphasis on the Linux/OSS communities part to even attempt it.
Re:Glibc getaddrinfo stack-based buffer overflow by golergka on Tuesday February 16, 2016 @10:16AM (#11110326)
Right now, companies that run linux on their servers don't have to pay huge fines when security meltdowns happen.

I imagine if they would, they would put more money into the problem: either fund linux development or created more of a market for commercial server operating systems (which can still be open source, at least in some interpretations of the term), for example.

Re:Glibc getaddrinfo stack-based buffer overflow by nitrogen on Tuesday February 16, 2016 @12:36PM (#11111576)
No. Strict liability for third party software failure is not a good idea. Software gains much much more from the freedom to innovate than it loses from exploits. Strict liability would be a terrible blow against basically everything HN represents.
Re:Glibc getaddrinfo stack-based buffer overflow by golergka on Tuesday February 16, 2016 @12:51PM (#11111724)
Well, there are areas where freedom to innovate is more important, and then there are areas where liability is more important.

May be US (as de-facto main government-level actor in IT and internet) could create voluntary certification that opened up companies to such liabilities? Small startup operating from a garage — no certification necessary, but users are aware that no one is guaranteeing anything. IBM? Level A certification which they advertise everywhere, with billions of dollars in potential fines.

Re:Glibc getaddrinfo stack-based buffer overflow by ivl on Tuesday February 16, 2016 @01:01PM (#11111789)
If it was voluntary, I doubt many of those companies would see it as worth the risk. The problem is that these errors happen because so many people are working on interdependent parts. Many times, it's not even your error that can make your software vulnerable. I dislike making anything so risky, because we'll see much, much less good software at the end of the day.
Re:Glibc getaddrinfo stack-based buffer overflow by ptaipale on Wednesday February 17, 2016 @04:13AM (#11116438)
Plus a culture of risk-avoidance, blame-shifting and bureaucracy-over-innovation.

I even feel the breath of having Ada (the language) enforced over all of us again, and similar ideas...

(Not that Ada is so bad as a language, it's more about the ideas of enforcement and top-down control...)

Re:Glibc getaddrinfo stack-based buffer overflow by pdonis on Tuesday February 16, 2016 @11:59AM (#11111247)
> Can we agree that it's urgently necessary to rewrite most of the core Linux/OSS stack in memory safe languages?

Why do "we" have to agree? If somebody really believes this is necessary, why don't they just do it?

Re:Glibc getaddrinfo stack-based buffer overflow by caf on Tuesday February 16, 2016 @09:25PM (#11114773)
Exactly right. The best vote you can cast for this - and the only one that matters - is to go out and rewrite something.
Re:Glibc getaddrinfo stack-based buffer overflow by lawnchair_larry on Tuesday February 16, 2016 @09:11PM (#11114691)
Sounds like a lot of work. I'd rather just agree.
Re:Glibc getaddrinfo stack-based buffer overflow by cmrx64 on Tuesday February 16, 2016 @09:59AM (#11110204)
I'm happy to have more contributors! ;) https://robigalia.org/
Re:Glibc getaddrinfo stack-based buffer overflow by efuquen on Tuesday February 16, 2016 @10:06AM (#11110250)
Re:Glibc getaddrinfo stack-based buffer overflow by mbreedlove on Tuesday February 16, 2016 @09:59AM (#11110199)
"It'll be slower than C", they'll say. However, I would gladly sacrifice performance for memory safety, at least in an alternate, opt-in, network stack.

Unfortunately, I doubt this will ever happen while Linus is around. He seems to be quite fond of C. I assume he would argue this is a matter of shitty developers, not a shitty language.

Re:Glibc getaddrinfo stack-based buffer overflow by jimktrains2 on Tuesday February 16, 2016 @10:01AM (#11110212)
Rust is actually a decent contender in this space. Memory safe and with the performance of C.

> I assume he would argue this is a matter of shitty developers, not a shitty language.

All developers are shitty.

Re:Glibc getaddrinfo stack-based buffer overflow by qznc on Tuesday February 16, 2016 @11:22AM (#11110886)
Rust also has nearly no runtime and no garbage collector. Lack of GC makes it possible to integrate a Rust library into scripting languages etc. This is an easier upgrade path from C, which D, Go, Nim, etc cannot provide.

(For my projects I prefer D, but Rust has a point here)

Re:Glibc getaddrinfo stack-based buffer overflow by nyan4 on Wednesday February 17, 2016 @05:30AM (#11116850)
Nim GC is optional; It can be disabled to write system libraries.
Re:Glibc getaddrinfo stack-based buffer overflow by denim_chicken on Tuesday February 16, 2016 @11:23AM (#11110892)
Rust is not a decent contender. It is far too immature. The only reasonable contender is C++.
Re:Glibc getaddrinfo stack-based buffer overflow by pyre on Tuesday February 16, 2016 @12:13PM (#11111360)
At what point will people consider Rust mature enough though? If we're going on number of years in existence, then C will always be older than Rust and the "well it's more 'battle-tested'" argument will never be false (for any language that is currently younger than C).
Re:Glibc getaddrinfo stack-based buffer overflow by denim_chicken on Tuesday February 16, 2016 @12:31PM (#11111530)
Maybe in a decade.
Re:Glibc getaddrinfo stack-based buffer overflow by yazaddaruvala on Wednesday February 17, 2016 @02:06AM (#11115906)
Do you not use git, because it hasn't been a decade? Will you only start using it after April 7th?

I've heard someone in real life say "git just isn't there yet", so I couldn't resist humoring myself with this comment, sorry.

Re:Glibc getaddrinfo stack-based buffer overflow by ufo on Tuesday February 16, 2016 @11:40AM (#11111057)
C++ is not reasonable if your goal is to avoid memory safety bugs.
Re:Glibc getaddrinfo stack-based buffer overflow by aikah on Tuesday February 16, 2016 @02:36PM (#11112521)
> C++ is not reasonable if your goal is to avoid memory safety bugs.

I'll get crucified for saying this but it's easier to write safe code in C++ than C. C requires a perfect discipline very few people can maintain.

Cyclone-lang had good ideas about how to fix C, almost 10 years ago. I wish it has been more successful. i was less "alien" looking than Rust.

Re:Glibc getaddrinfo stack-based buffer overflow by pjmlp on Tuesday February 16, 2016 @05:00PM (#11113457)
True, and hence why my rants about C tend to kind of ignore C++, as the community is more safe aware and strives to avoid the C compatibility subset.

However, it is very hard to avoid C++ developers that don't follow the modern C++ mantra to use it as if it was C, specially in the enterprise space.

You end up with "C compiled with C++" code style, which usually isn't subjected to any kind of code reviews or static analysis.

Re:Glibc getaddrinfo stack-based buffer overflow by denim_chicken on Tuesday February 16, 2016 @12:30PM (#11111527)
Neither is Rust, which comes with a huge RTS that itself likely full of such bugs. The library and the compiler too may have bugs that compromise the "guaranteed" memory safety of a libc written in Rust.

There is no silver bullet here. If one takes into account all the sources of bugs, C may very well be the best choice given its maturity and its ecosystem. Our best bet is likely to put more resources towards glibc development and especially fuzz testing. I bet there are only 1-2 developers who work on glibc full-time.

Re:Glibc getaddrinfo stack-based buffer overflow by arielb1 on Tuesday February 16, 2016 @01:32PM (#11112033)
Rust does not really have an RTS per se. libstd is just a standard library.

Also, even if rustc has soundness bugs, you won't find them if you don't look for them, and even then, just trying to make your program fit the types will prevent most bugs.

By the way, glibc's code is too horrible to be read - musl is a more modern alternative.

Re:Glibc getaddrinfo stack-based buffer overflow by h3st on Wednesday February 17, 2016 @09:55AM (#11118200)
> Rust does not really have an RTS per se. libstd is just a standard library.

And if you don't want to use the standard library, you just say #![no_std] https://doc.rust-lang.org/stable/book/using-rust-without-the...

Re:Glibc getaddrinfo stack-based buffer overflow by mikeash on Tuesday February 16, 2016 @12:59PM (#11111780)
Has there been any work on a safe C? Not a C-ish language that is safe, but an actual implementation of the C language itself.

The C standard doesn't require the compiler to check the validity of array indexing and pointer dereferences and such, but it also doesn't require the compiler not to. Can they be added without destroying performance and compatibility?

Some of this already exists, with stuff like stack canaries and address sanitizer. But can it go beyond these ad hoc solutions that catch common mishaps, and become something truly reliable?

It should be possible to just flip a switch on the compiler and have it so that, for example, if you alloca() X bytes and then use the resulting pointer to access byte X+1, that is reliably caught at runtime. And not just as some single patch that specifically looks out for alloca() abuse, but as part of a wider system that understands and catches all out of bounds access.

Re:Glibc getaddrinfo stack-based buffer overflow by GregBuchholz on Tuesday February 16, 2016 @03:40PM (#11112971)
Have you looked at CCured?

http://www.cs.berkeley.edu/~necula/Papers/ccured_pldi03.pdf

"CCured is a program transformation system that adds memory safety guarantees to C programs by verifying statically that memory errors cannot occur and by inserting run-time checks where static verification is insufficient."

Re:Glibc getaddrinfo stack-based buffer overflow by mikeash on Wednesday February 17, 2016 @09:56AM (#11118204)
I haven't heard of it before, but that's the sort of thing I'm thinking of, and that looks pretty cool. Looks like the resulting programs still perform reasonably well too.
Re:Glibc getaddrinfo stack-based buffer overflow by steveklabnik on Tuesday February 16, 2016 @01:58PM (#11112240)
This is sort of what Cyclone tried, in a way.
Re:Glibc getaddrinfo stack-based buffer overflow by mikeash on Tuesday February 16, 2016 @02:06PM (#11112287)
Cyclone looks cool, but it's a pretty significant departure from normal C. I'm wondering how far you can take safety without changing the actual language at all. Or rather, how far you can take safety without changing the language and without completely killing performance. Memory safety shouldn't be hard: track all valid regions and pointers to them and don't allow mixing stuff up. But doing it fast is tough.
Re:Glibc getaddrinfo stack-based buffer overflow by openasocket on Tuesday February 16, 2016 @01:13PM (#11111860)
FYI, Rust has no more of a runtime system than C/C++.
Re:Glibc getaddrinfo stack-based buffer overflow by AsyncAwait on Tuesday February 16, 2016 @02:50PM (#11112638)
That doesn't mean sticking with the status-quo is the solution.

Rust certainly looks like a better starting point for modern, safe low-level programming than C. C served us quite well for over 40 years, but perhaps it's time to move on...

Re:Glibc getaddrinfo stack-based buffer overflow by thrownaway2424 on Tuesday February 16, 2016 @10:23AM (#11110380)
Your comment was inevitable, but can you also include the options of rewriting in any reasonable form? I'd settle for c++ or even readable c. The problem with this code isn't necessarily the language, it's that it is an unreadable, unreviewable mess.
Re:Glibc getaddrinfo stack-based buffer overflow by peterwwillis on Tuesday February 16, 2016 @10:32AM (#11110462)
It's not urgently necessary since all modern systems have some form of simple address space randomization at prelink or exec.

Considering the time and effort it would take to safely and completely replace glibc with a new implementation, and the limited scope of its effect on Internet-wide security, it would be more productive to require the kernel itself to support patches like PaX, Exec Shield, or grsec in general, so that all applications (not just ones made with a specific language) would have stack-smashing protection.

Re:Glibc getaddrinfo stack-based buffer overflow by lmm on Tuesday February 16, 2016 @10:35AM (#11110490)
None of those things is reliable. They make it harder to exploit this kind of vulnerability, but not impossible; usually they just add one extra required step to the exploitation.
Re:Glibc getaddrinfo stack-based buffer overflow by peterwwillis on Tuesday February 16, 2016 @10:52AM (#11110639)
Yes. It's called a mitigation, and it's what we do to make systems more secure. But they certainly can be reliable.

The Internet has been using DNS resolvers known to be vulnerable to cache poisoning birthday attacks for at least 14 years. We add basic defenses that prevent the majority of attacks, and trudge on, rather than fix the protocol itself, which is a big complicated mess. The smaller, less effective fixes reach a broader number of use cases with less effort, and so we get along okay.

Likewise, we know that no matter what language we use, there will be at some point a problem with regard to accessing memory, and so we put protections in place to limit the scope of those attacks in a broad sense. It's a more effective mitigation for a greater number of use cases. It is not a panacea.

Re:Glibc getaddrinfo stack-based buffer overflow by lmm on Tuesday February 16, 2016 @11:02AM (#11110716)
I find that a lot of security vulnerabilities result from confusion about what side of a security boundary something is on. Treating a parameter as sorta-untrusted can be actively harmful if it leads someone to believe that parameter was supposed to be untrusted. I think it's much more effective to concentrate on strict, designed security boundaries that stop 100% of attacks than to create a bunch of ad-hoc 80% barriers.
Re:Glibc getaddrinfo stack-based buffer overflow by sargun on Tuesday February 16, 2016 @10:26AM (#11110417)
I'm still unsure why people start projects with C, or C++ (or other unsafe languages), that do not need it. I totally understand that there are use cases where C++ and C make sense.

In my mind, these use cases are quite limited though - integration into 3rd party libraries, legacy codebases, and working in embedded environments. I also agree that performance can be a reason, but see Knuth's opinion on premature optimization.

I also think that when writing C, and C++ it makes a lot of sense to restrict oneself as much as possible a la the kinds of standards in the Google Style Guide, or NASA's style guide.

Can people tell me why they start projects in broad C++, and C still? I may be completely missing some piece of the puzzle.

Re:Glibc getaddrinfo stack-based buffer overflow by tangent128 on Tuesday February 16, 2016 @11:09AM (#11110777)
C is still the lingua franca; it's the only toolchain you can rely on a target system having, and the only real usable interface for calling between code in different languages.

So my theory is that it's not so much that people are especially inclined towards starting projects in C, but that projects in C have fewer barriers to distribution and thus adoption.

By natural selection, C code spreads and becomes standard; programs in Java, JS, Ruby, Haskell, Ocaml, etc. don't tend to leave their native ecosystem.

Re:Glibc getaddrinfo stack-based buffer overflow by groovy2shoes on Tuesday February 16, 2016 @12:24PM (#11111474)
This is exactly my reason for starting a couple of recent side-projects in C. You can pretty much count on the target system having an ANSI C compiler (even rather old systems), which can't be said for any other language. Have an ANSI C compiler? Great! You don't need anything else to build and install this program. You don't need to install a hefty runtime like the JVM and you don't need to know what a gem is.

I don't think I'm immune to the hazards inherent in the language. I try my best be aware of them and to write safe code, and still find myself introducing a segfault now and then. Luckily, tools like valgrind and Coverity can really help to catch these kinds of issues early (of course, they can't catch everything, but I've been amazed by just how much they do catch).

Re:Glibc getaddrinfo stack-based buffer overflow by autoreleasepool on Tuesday February 16, 2016 @11:50AM (#11111167)
> Can people tell me why they start projects in broad C++, and C still? I may be completely missing some piece of the puzzle.

Sure.

---

Compelling features:

- Unrivaled portability (yes, contrary to folklore C - and to a lesser extent C++ - is more portable than Java)

- Excellent libraries

- Excellent documentation and learning resources

- Unrivaled tooling

- perfect mapping to how the computer works

- Unrivaled performance (other than Rust)

- exceptional, engaging, and intelligent community (specifically referring to the cpp community)

---

Why start a C++ project today?

- you want to write a web browser

- you want a platform agnostc GUI

- you want to learn OpenGL or Vulkan

- you want to use Unreal and you're not a visual learner

- you're want to write the business logic for a cross platform application and share as much code as possible

- you're implementing you're own programming language

- your implementing an algorithm or data structure you've been studying

- you're bored

- you like programming in C++

- you want to learn template meta-programming

- you want to learn functional programming

- you want to learn how to write a device driver

- you need the performance

- you are not writing mission critical software (aka 99% of all side projects)

- you're a curious person

- you're a contrarian and everyone you know uses Java

- you want to become a more well-rounded engineer

- you want to learn about the ins and outs of language design and features

- you want make a lot of money

- you only know C++

Those are a few reasons. Hope that helps you solve your puzzle.

PS: Do the anti-C people realize that a kernel in Rust/Nim/whatever would be under a big unsafe block anyways? Yeah, you have to program "unsafe" code eventually. Those nice high level abstractions are not omnipresent, you know. They need something to abstract.

Re:Glibc getaddrinfo stack-based buffer overflow by greiskul on Tuesday February 16, 2016 @04:52PM (#11113396)
Just like a kernel written in C is a big block of assembly code, right? You can isolate any unsafe blocks into small functions, with the smallest scope needed. Same as you would do for asm blocks in C. This is good because it calls attention to what you are doing, when reviewing any code with an unsafe block, you remind yourself to check for these kinds of bugs very carefully, unlike when you are reviewing generic C code where almost every line can have a memory bug.
Re:Glibc getaddrinfo stack-based buffer overflow by steveklabnik on Tuesday February 16, 2016 @01:48PM (#11112145)

  > a kernel in Rust/Nim/whatever would be under a big unsafe block anyways?
Only portions of it do. The whole point of unsafe is that you can encapsulate it, and then go from there.
Re:Glibc getaddrinfo stack-based buffer overflow by autoreleasepool on Tuesday February 16, 2016 @02:00PM (#11112249)
Definitely. The ability to encapsulate code that is less strictly checked by the compiler is what makes Rust such a compelling alternative to C. I definitely think Rust is the ideal choice for a hobby kernel from scratch in 2016.

My point is that unsafety is unavoidable. You can encapsulate unsafe Rust or wrap plain old C, it's, for all practical purposes, the same. At that level language features matter a lot less. I agree we should minimize and encapsulate loosely checked code as best we can.

Re:Glibc getaddrinfo stack-based buffer overflow by steveklabnik on Tuesday February 16, 2016 @02:08PM (#11112312)
Yeah, absolutely. Ultimately, the machine is not safe. Such is life.
Re:Glibc getaddrinfo stack-based buffer overflow by bcook on Tuesday February 16, 2016 @08:14PM (#11114438)
Exactly... and users are even worse.
Re:Glibc getaddrinfo stack-based buffer overflow by pjmlp on Tuesday February 16, 2016 @10:45AM (#11110571)
Sometimes there isn't any other choice.

For example, in the mobile OS space if you want to write native apps, without rewriting 100% of the application code per platform and avoid toolchain debugging headaches, C and C++ are the only languages available in iOS, Android and WP SDKs.

In WP case, there are APIs like DirectX that aren't fully exposed via WinRT, so you need to write a C++ wrapper for those type of APIs.

There are other options, but they are either commercial (e.g. Xamarin), or require lots of DYI hacking with the SDKs toolchains.

So if you want to remain productive and not pay for the commercial solutions, there aren't any other alternatives.

Which means, even I with my safety rants, end up using them for my mobile OS hobby coding.

However if I would be doing this commercially, Xamarin would be my first option.

Re:Glibc getaddrinfo stack-based buffer overflow by tosseraccount on Tuesday February 16, 2016 @11:42AM (#11111070)
Isn't slapping C onto objective C trivial for iOS and OSX? Just wrap to the GUI's objective C. You can use this trick for WTL on Window's C++, too.
Re:Glibc getaddrinfo stack-based buffer overflow by jsmthrowaway on Tuesday February 16, 2016 @12:28PM (#11111508)
People seem to forget that Objective C is a superset of C. No slapping on required. You're already writing it. Avoid brackets and a few other things and it's C.

You don't even have to rename .m to .c, but you can.

Re:Glibc getaddrinfo stack-based buffer overflow by creshal on Tuesday February 16, 2016 @11:09AM (#11110778)
> I'm still unsure why people start projects with C, or C++ (or other unsafe languages), that do not need it. I totally understand that there are use cases where C++ and C make sense.

On Linux it's easy: It is (or was, until very recently) the least sucky alternative. All others have (or had) issues with OS support (C#), distribution (Java: OpenJDK vs. Oracle JDK; Python: 2.5 to 3.4 parallel in use by different distributions; Vala: Every compiler update breaks the language; etc. pp.), availability of libraries (Ruby, PHP, Perl… for anything that's not their speciality) or general obscurity (OCaml, …).

Go and Rust (and JS, C# and Vala recently) seem to be changing this, thankfully. But those too will depend on the availability of high-quality bindings for all the C(++) libraries you have to use to get anything substantial done.

Re:Glibc getaddrinfo stack-based buffer overflow by d0lph on Tuesday February 16, 2016 @11:21AM (#11110875)
Written properly, C++ is not unsafe.

Certain language own certain domains, for better or worse, this means that most developers interested in low level programming will know C/C++, and that the will be a substantial amount of tooling already in place.

Re:Glibc getaddrinfo stack-based buffer overflow by cmrx64 on Tuesday February 16, 2016 @11:41AM (#11111062)
> Written properly, C++ is not unsafe.

This is abusing what "unsafe" means. As a language, C++ is inherently unsafe. However, some C++ programs ("written properly") are correct.

If written properly C++ is not unsafe, then written properly C is not unsafe.

Re:Glibc getaddrinfo stack-based buffer overflow by SCHiM on Tuesday February 16, 2016 @12:28PM (#11111504)
No I think he meant that if you adhere to certain language constructs the language is just as memory safe as Rust/others.

Eg. Don't use C++ as C with vectors, use C++ with the more modern language features:

It's the difference between:

    // where f is of type fstream of a file containging "this is a string\r\n"No I think he meant that if you adhere to certain language constructs the language is just as memory safe as Rust/others.
Eg. Don't use C++ as C with vectors, use C++ with the more modern language features:

It's the difference between:

    std::string A = "ab" + std::string("c");

    // And:
    char Buffer[512] = {};
    strcat(Buffer, "ab");
    strcat(Buffer, "c");
Or alternatvily:

    std::vector<uint8_t> SomeData = { 1,2,3 }
    std::vector<uint8_t> OtherData(SomeData.begin(), SomeData.end());

    // and:
    std::vector<uint8_t> SomeData = { 1,2,3 }
    std::vector<uint8_t> OtherData;
    OtherData.reserve(3);
    memcpy(...)

In the first one you're 'protected' by the stl code, which is arguably the same level of protection as afforded by any memory safe language’s library code.

    std::string A = "ab" + std::string("c");

    // And:
    char Buffer[512] = {};
    strcat(Buffer, "ab");
    strcat(Buffer, "c");
Or alternatvily:

    std::vector<uint8_t> SomeData = { 1,2,3 }
    std::vector<uint8_t> OtherData(SomeData.begin(), SomeData.end());

    // and:
    std::vector<uint8_t> SomeData = { 1,2,3 }
    std::vector<uint8_t> OtherData;
    OtherData.reserve(3);
    memcpy(...)

In the first one you're 'protected' by the stl code, which is arguably the same level of protection as afforded by any memory safe language’s library code.
Re:Glibc getaddrinfo stack-based buffer overflow by dbaupp on Tuesday February 16, 2016 @01:39PM (#11112077)
> No I think he meant that if you adhere to certain language constructs the language is just as memory safe as Rust/others.

It's not; as soon as code uses (for instance) references, you're running the memory-unsafety gantlet. E.g. even something super-modern like

  for (auto& thing: vector)
is risky: what if the loop body happens to somehow call `vector.push_back(x)`?
Re:Glibc getaddrinfo stack-based buffer overflow by tomjakubowski on Tuesday February 16, 2016 @02:35PM (#11112513)
> is risky: what if the loop body happens to somehow call `vector.push_back(x)`?

Some people like to say that these bugs are trivial to avoid ("who would write code like that?!"), but they miss the insidious (and common) case: when code several layers down the call stack (in some arbitrary callback assigned at runtime, say) grows the vector/map from under you.

Re:Glibc getaddrinfo stack-based buffer overflow by kccqzy on Wednesday February 17, 2016 @02:44AM (#11116063)
It's still easy to avoid though. Just take a const reference to the vector and use that in the loop and pass that to the callback. It just requires discipline, and in fact less discipline than writing C.
Re:Glibc getaddrinfo stack-based buffer overflow by gpderetta on Tuesday February 16, 2016 @12:36PM (#11111579)
I don't subscribe to the "properly written Modern C++ is safe" meme, but still it must be recognized that there are degrees of unsafety [1] between languages.

[1] not talking about memory safety, but as a general "probability of having a defect".

Re:Glibc getaddrinfo stack-based buffer overflow by cmrx64 on Tuesday February 16, 2016 @01:44PM (#11112119)
I really dislike the "safe"/"unsafe" terminology, there isn't a commonly-agreed upon definition of "safe". But I think it's safe to say that it's a combination of type and memory safety, not "low probability of having a defect".

There certainly is a spectrum.

Re:Glibc getaddrinfo stack-based buffer overflow by tosseraccount on Tuesday February 16, 2016 @11:36AM (#11111014)
Because you can't release a single file statically compiled native binary that relies only on established system calls. People typically can't install packages anymore. They can grab an exe.
Re:Glibc getaddrinfo stack-based buffer overflow by w8rbt on Tuesday February 16, 2016 @12:32PM (#11111534)
This is the reason I ported several Python and Java apps to C++. One exe and it just works.
Re:Glibc getaddrinfo stack-based buffer overflow by J_Darnley on Tuesday February 16, 2016 @03:06PM (#11112752)
> Can people tell me why they start projects in ... C still?

Okay. Based on my current project (Porting AVS out of shit-tier Microsoft C++). I want a region of memory in which I can treat as a bitmap and set pixels at given location to given colours. I know of no other language that gives me this block of memory to draw into. That's probably because I don't know enough. Most languages seem to tout how immutable things are. That's a big word for many people and it just means "does not change".

If other languages want my attention then maybe they need more than a "Hello, World!" and some string processing. Show me your malloc equivalent and see if I can go from there.

Re:Glibc getaddrinfo stack-based buffer overflow by GregBuchholz on Wednesday February 17, 2016 @12:00PM (#11119247)
>I want a region of memory in which I can treat as a bitmap... ...Show me your malloc equivalent and see if I can go from there.

http://www.lispworks.com/documentation/HyperSpec/Body/f_mk_a...

https://wiki.haskell.org/Numeric_Haskell:_A_Vector_Tutorial#...

Re:Glibc getaddrinfo stack-based buffer overflow by J_Darnley on Wednesday February 17, 2016 @02:46PM (#11120669)
Thank you.