News for hackers. Stuff that matters.
Re:Remote code execution, git, and OS X by pjmlp on Monday April 18, 2016 @03:34AM (#11518283)
Best part:

> So, what's the big deal? Crappy C code gets exploited every day, and we upgrade it, and then we're "safe" until the next huge hole that's been there forever is reported. (In the meantime, people party with their private stash of vulnerabilities.)

Re:Remote code execution, git, and OS X by blub on Monday April 18, 2016 @04:26AM (#11518437)
I looked at the offending function, and it would be trivial to rewrite it in C++ in an easy to understand and safer way. It probably wouldn't be as safe as Rust, but still a lot better.

A lot of Linux C utilities would benefit from such a treatment.

Re:Remote code execution, git, and OS X by lmm on Monday April 18, 2016 @05:58AM (#11518741)
If you're going to rewrite in any case surely it's worth going to Rust (or Ada, or another language that simply doesn't have the myriad unsafe possibilities that C++ does).
Re:Remote code execution, git, and OS X by blub on Monday April 18, 2016 @07:00AM (#11518933)
There are many arguments to be made in favor of C++, such as talent availability, experience from other porting projects, the possibility of doing incremental porting, etc.

My impression is that some projects are already experimenting with or using C++ in their C code bases, so C to C++ is quite likely.

Re:Remote code execution, git, and OS X by dikaiosune on Monday April 18, 2016 @12:21PM (#11521072)
Incremental porting is possible in Rust. There are a few ways to go about it, and it is pretty easy to link Rust and C code together.
Re:Remote code execution, git, and OS X by blub on Monday April 18, 2016 @01:43PM (#11521739)
Most languages have some C interop, and that satisfies the minimal definition of "incremental".

However, using C and C++ together in a project is especially easy. If I were to do this, I would first get the code base compiling with a C++ compiler (this already brings some extra type safety) and is not particularly difficult.

Then I'd start replacing C code blocks with safer C++ code. This could mean changing a function, some parameter-passing conventions, replacing char* with std::string, etc.

This has the biggest chance of success I feel, and there's already success stories and strategies available that describe this method. E.g: GCC.

Re:Remote code execution, git, and OS X by dikaiosune on Monday April 18, 2016 @02:06PM (#11521920)
Rust <-> C interop is pretty strong. AFAIK, there is equivalent call overhead between Rust & C as there is between C & C++ (i.e. none). You're right that incrementally porting to Rust would be a little bit more overhead than C++, simply because you'd need two compilers, and instead of rewriting function definitions in-place you'd need to write a fresh function in Rust and delete it in C. But at link time everything is sane and once you set up the build, it's really not hard.

I'm not suggesting that C -> Rust is easier than or quite as easy as C -> C++, just that it is much easier than C -> most other languages, and that it is close enough to C -> C++ that it is worth investigating. It is definitely more robust than the minimal definition of "incremental."

Re:Remote code execution, git, and OS X by zaphar on Monday April 18, 2016 @09:49AM (#11519829)
But if the goal of the rewrite is safer tooling then Rust wins.
Re:Remote code execution, git, and OS X by falcolas on Monday April 18, 2016 @10:00AM (#11519898)
Or Python, or Ada, or Ruby, or Go, or Java, or Lisp, or any of the myriad of other memory safe programming languages.

When it comes to memory safe languages, your choices do not boil down to "Rust or nothing".

Re:Remote code execution, git, and OS X by blub on Monday April 18, 2016 @10:47AM (#11520296)
No, in this case it pretty much does boil down to Rust or C++. This is a low-level tool, not a webapp.
Re:Remote code execution, git, and OS X by falcolas on Monday April 18, 2016 @11:06AM (#11520464)
That still gives you Lisp, Ada, Go, Haskell, and Java, if you make the (imo incorrect) assumption that "low level" tools can only be written in languages which compile down to bytecode.

Of course, Mercurial gives lie to this assumption.

Re:Remote code execution, git, and OS X by samirm on Monday April 18, 2016 @01:41PM (#11521718)
Java? You won't get any performance out of it compared to something like C.
Re:Remote code execution, git, and OS X by joushou on Monday April 18, 2016 @03:23PM (#11522456)
I don't like Java, but don't underestimate the performance of the JVM. Unless you're a crazy perf wiz, then your average C code won't beat your average Java code. It's fast enough for short processes, and for long processes the JIT is pretty darn good. Also note that a JIT can perform runtime assumptions and optimize code based on what is currently the case, which an AOT compiler cannot.

It takes a lot more than a toolchain to write fast code.

Re:Remote code execution, git, and OS X by pjmlp on Monday April 18, 2016 @02:16PM (#11521997)
In what scale?

What it matters is if it is fast enough for the use case being targeted.

As side note I remember when C compilers for home computers generated worser code than junior Assembly programmers.

Re:Remote code execution, git, and OS X by pjmlp on Monday April 18, 2016 @11:40AM (#11520766)
A command line tool like Git can be written in any programming language that has implementations capable of generating native code.

Even then, Mercurial is implemented in Python and quite usable

Re:Remote code execution, git, and OS X by bsder on Monday April 18, 2016 @02:50PM (#11522240)
Git is not a "low-level" tool.

The fact that a Java rewrite of git actually exists demonstrates the falsity of this statement.

Re:Remote code execution, git, and OS X by ptc on Monday April 18, 2016 @11:32AM (#11520703)
@falcolas -- I think you mean machine code not bytecode. And w.r.t. Mercurial afaik the project's hot paths are all written in Cython extensions, and there's ongoing work to improve the Python part by working with the PyPy folks. So, there's definite technical advantages in using Python for greater developer productivity, but there's also a cost.
Re:Remote code execution, git, and OS X by zaphar on Monday April 18, 2016 @01:17PM (#11521520)
I was not implying it did. I was responding to the C++ suggestion specifically.
Re:Remote code execution, git, and OS X by blub on Monday April 18, 2016 @10:49AM (#11520315)
The goal is writing safer code, but that also comes with some costs. The interesting question is which costs can the project afford.

Otherwise yes, it could even be rewritten in Ruby, as falcolas suggested...

Re:Remote code execution, git, and OS X by lmm on Monday April 18, 2016 @07:11AM (#11518973)
My impression is that projects that mix C and C++ tend to use the C-like subset of C++ (with classes), so they wouldn't gain any safety advantage from moving to C++.

Many other languages support C linkage in a way that's comparable to C++.

Re:Remote code execution, git, and OS X by blub on Monday April 18, 2016 @08:23AM (#11519325)
Using std::vector, std::string and smart pointers would already bring large benfits, so even a C-like subset could work fine.

The point is that code like in that function is a nightmare to write correctly and test in plain C.

Re:Remote code execution, git, and OS X by pjmlp on Monday April 18, 2016 @08:37AM (#11519408)
I agree with you, the only problem being that even in 2016 there are shops that prohibit the usage of STL.

The real way out is to eventually change to a language where safety is opt-out and not opt-in, like in C++.

Re:Remote code execution, git, and OS X by w8rbt on Monday April 18, 2016 @10:20AM (#11520059)
C++ is far safer than C precisely for the reasons listed. The standard is safe by default (it's not opt-in). If you want or need backward compatibility with C, then you can use the more error prone C constructs for that. Otherwise, pure C++ code is safe.
Re:Remote code execution, git, and OS X by pjmlp on Monday April 18, 2016 @10:50AM (#11520317)
It is opt-in, because it depends on the STL, which many shops forbid.

I have always been on the C++, in the C vs C++ wars, but I am also aware of all those developers that just code C with a C++ compiler, hence opt-in.

Re:Remote code execution, git, and OS X by ryandrake on Monday April 18, 2016 @01:57PM (#11521846)
I'd hope people are not using the STL nowadays, in favor of the C++ Standard Library which is part of the C++ standard.
Re:Remote code execution, git, and OS X by pjmlp on Monday April 18, 2016 @02:35PM (#11522138)
People like myself that know C++ since the "C++ Annotated Reference Manual" tend to keep using STL to designate the standard library, but I guess you already knew that.

If it makes you happy I can use the ANSI C++ section number instead.

Re:Remote code execution, git, and OS X by ryandrake on Monday April 18, 2016 @03:40PM (#11522582)
Wasn't sure if that's were you were going. So, who in this day and age, forbids the use of the standard library? That seems pretty bizarre. What would be the motivation behind such a policy?
Re:Remote code execution, git, and OS X by pjmlp on Monday April 18, 2016 @04:28PM (#11522938)
The arguments against it are usually that it is too bloated, too slow, makes use of templates and exceptions.

So any place that is against templates and exceptions, usually rules out the standard library on those arguments.

Then you have the software houses, whose C++ code is actually C with a C++ compiler that use the bloat and slow arguments against the library.

I don't remember them by heart, but there were a couple CppCon 2014 talks where this type of arguments was discussed.

Re:Remote code execution, git, and OS X by ryandrake on Tuesday April 19, 2016 @11:28AM (#11527629)
Whenever someone complains to me that the standard library is slow or "bloated" (whatever that means), I ask if they've ever profiled their code vs. the standard library version. 9 times out of 10 they have not, and are operating out of mythology rather than measurement.
Re:Remote code execution, git, and OS X by pjmlp on Monday April 18, 2016 @08:31AM (#11519372)
That is my feeling as well.

I do like to use C++ a lot on personal projects, but there I can make full use of C++ best practices.

At work, I tend to avoid using it, because most C++ developers I have met on my career, actually use it as Better C, keeping all safety loopholes from C.

I had my share of spending weeks tracking down memory corruption issues.

Re:Remote code execution, git, and OS X by kaffeinecoma on Monday April 18, 2016 @10:26AM (#11520106)
Re-writing Git in C++? Can't tell if you're trolling or not. http://harmful.cat-v.org/software/c++/linus
Re:Remote code execution, git, and OS X by pjmlp on Monday April 18, 2016 @10:44AM (#11520266)
Yet given the actual state of Gtk development, even Linus is using C++ nowadays via Qt.

https://github.com/torvalds/subsurface

Re:Remote code execution, git, and OS X by geofft on Monday April 18, 2016 @11:20AM (#11520588)
Argument 1 is nontechnical and by someone who does not have a lot of experience writing userspace software.

Argument 2 is valid, but only applies to kernels, and also only applies to 1992 (it works a lot better now).

Re:Remote code execution, git, and OS X by kaffeinecoma on Monday April 18, 2016 @12:29PM (#11521147)
My comment wasn't meant to knock C++; it was to point out that Linus' opposition to the language is well-documented, and an official git-rewrite in C++ is very unlikely.
Re:Remote code execution, git, and OS X by geofft on Monday April 18, 2016 @12:52PM (#11521315)
Linus is almost entirely uninvolved with git these days, though. `git log --author=Torvalds` finds no commits in 2015 or so far this year, three patches in 2014, none in 2013, five in 2012, four in 2011... if the git core team felt using C++ was a possible course, Linus would not be involved in the decision.

(That's probably why I misunderstoood your statement an argument against C++ on its own merits.)

Re:Remote code execution, git, and OS X by joushou on Monday April 18, 2016 @05:43AM (#11518697)
A rewrite in C++ would not make it any safer. Rewriting it to make it easier to understand could make it safer, but you do not need a different language for that.
Re:Remote code execution, git, and OS X by claudius on Monday April 18, 2016 @07:42AM (#11519112)
If you can change the name_path struct to a standard STL data structure (std::vector<std::string> might work just fine), you can easily rewrite the function such that all memory allocation and all exploitable memory access is delegated to STL containers. If your STL std::string has a buffer overflow issue, you’re of course still in trouble, but the same is true if there are bugs in the STL/implementation of Go/Rust/Python/Java/Haskell…

Something like:

std::string path_name(std::vector<std::string> const& dirs, std::string const& name) { std::string p; for(auto const& dir : dirs) { p += (dirs + "/"); } p += name; return p; }

should work. If you feel like it, you can also add something like:

  std::size_t len = name.size();
  for(auto const& dir : dirs) { len += dir.size(); }
  p.reserve(len);
Of course, len may overflow, but even if it does, all the harm that causes is that the string will have to reallocate memory during growth until running in a segfault when further memory allocation fails.
Re:Remote code execution, git, and OS X by joushou on Monday April 18, 2016 @04:13PM (#11522822)
Right, and you can do the same with C, using libraries providing this functionality wrapped as well. There's plenty of std::string and std::vector like containers, which handle the magic for you. But even then, you can work with struct {len int; void* data;} to get your vector, and replace void* with char* to get a string. A simple vector and simple string is in no way difficult to implement, and many implementations exist.

I'm just trying to point out that the convenience of some C++ standard library features is not isolated to C++, and C++ is not a "memory safe" language by meaning of the word.

Re:Remote code execution, git, and OS X by claudius on Tuesday April 19, 2016 @04:01AM (#11525519)
The difference is that using STL containers such as std::string or std::vector are very much the default in C++ (much the same way ‘safe’ code is the default in Rust), whereas you have to do some manual work in C to get them. The result is that using std::string and std::vector here is the natural solution in C++, whereas likely very experienced C programmers stuck to the manual approach.
Re:Remote code execution, git, and OS X by smitherfield on Monday April 18, 2016 @11:35AM (#11520731)
I think you have a typo in your first code block.

p += (dir + "/");

Re:Remote code execution, git, and OS X by claudius on Monday April 18, 2016 @11:43AM (#11520796)
Oh, yes, sorry and now it’s too late to fix (both that and the horrible formatting). Though I suppose it would have been a compile-time error, so at least it shouldn’t be exploitable :')
Re:Remote code execution, git, and OS X by smitherfield on Monday April 18, 2016 @01:11PM (#11521469)
No worries! :)
Re:Remote code execution, git, and OS X by blub on Monday April 18, 2016 @07:02AM (#11518939)
Yes it would, because this is basically string processing where C is the worst possible language one could use. I've detailed in another comment how std::string could be used.
Re:Remote code execution, git, and OS X by joushou on Monday April 18, 2016 @07:28AM (#11519046)
You're talking about naked char[]. You can just as easily make a struct{ int len; char* str; } in C, and combined with the "n" variants of string operations, would work just fine with common tools.

Again, C++ does not make anything safer, and its types can easily be replicated in any other language.

Re:Remote code execution, git, and OS X by pjmlp on Monday April 18, 2016 @08:35AM (#11519400)
Being possible, doesn't mean the majority of C developers make use of it.

The "n" variants are a joke in terms of security, even the C99 annex, that was demoted to optional in C11.

I call them a joke, because tracking the pointer and length separately is hardly an improvement in terms of security.

The only improvement that the "n" variants added is that the null character is always added to the end, instead of how strncpy does it, by only adding the character if there is enough space.

Re:Remote code execution, git, and OS X by kccqzy on Monday April 18, 2016 @08:28AM (#11519353)
I believe this is called pascal style strings and GCC has builtin support for it.
Re:Remote code execution, git, and OS X by NoGravitas on Monday April 18, 2016 @07:42AM (#11519109)
You could stay in C and use glib strings.
Re:Remote code execution, git, and OS X by EGreg on Monday April 18, 2016 @11:08AM (#11520494)
Linus would chew you out for saying that son.

http://harmful.cat-v.org/software/c++/linus

Re:Remote code execution, git, and OS X by steveklabnik on Monday April 18, 2016 @10:09AM (#11519956)
Since we're talking about git, here's an interesting PR: https://github.com/libgit2/libgit2/pull/3540

(EDIT: Some stuff has changed since February, so some of these claims are now out of date. But not all of them, the overall situation is still similar.)

Re:Remote code execution, git, and OS X by userbinator on Monday April 18, 2016 @06:05AM (#11518758)
Rewriting in any language won't help with this one unless the language has builtin integer overflow checking.

The solution here is actually quite trivial: just restrict filenames to 255 bytes and nesting to 255 levels, which limits paths to 64KB at most. Anyone trying to use git repositories exceeding either of those limits should be considered insane.

Re:Remote code execution, git, and OS X by blub on Monday April 18, 2016 @06:55AM (#11518917)
The trick is to not do any index calculation.

I think that using std::string would prevent the bug, because it would throw length_error on append. string::resize can be used to avoid excessive allocations.

Sanity checks would provide extra safety, but the code should fail cleanly even without them.

Re:Remote code execution, git, and OS X by trywerwerwer on Monday April 18, 2016 @06:20AM (#11518797)
> just restrict filenames to 255 bytes and nesting to 255 levels, which limits paths to 64KB at most. Anyone trying to use git repositories exceeding either of those limits should be considered insane.

Funny, because I've actually been locked up for being insane five times. Speaking from experience, they'd only consider me insane if I said something like 255 was an important number because there are two sides to every problem and five fingers on each hand, and the path, which two feet take, is six one way / half a dozen the other, to the four corners of the earth, which is the natural limit.

So... you don't really know what insane is. Jus' sayin'.

Re:Remote code execution, git, and OS X by chris_wot on Monday April 18, 2016 @06:50AM (#11518899)
As someone who has also spent time in a mental institution, I just want to say that I really think you need to be less sensitive on this one. That's just an expression and not in any way aimed at people who have mental illness.
Re:Remote code execution, git, and OS X by kuschku on Monday April 18, 2016 @02:09PM (#11521938)
Accidentally downvoted you, I’m so sorry :/
Re:Remote code execution, git, and OS X by chris_wot on Monday April 18, 2016 @02:14PM (#11521982)
All good :-)