|
|
Log in / Subscribe / Register

Remote code execution vulnerability in Samba

The Samba 4.1.17, 4.0.25 and 3.6.25 releases are available; they fix an unpleasant code-execution vulnerability. See this Red Hat security blog entry for more information. "CVE-2015-0240 is a security flaw in the smbd file server daemon. It can be exploited by a malicious Samba client, by sending specially-crafted packets to the Samba server. No [authentication] is required to exploit this flaw. It can result in remotely controlled execution of arbitrary code as root."

to post comments

Remote code execution vulnerability in Samba

Posted Feb 23, 2015 20:54 UTC (Mon) by proski (guest, #104) [Link] (67 responses)

I hope all vulnerabilities are analyzed and lessons are learned. That's an error that even seasoned programmers can easily make. An uninitialized pointer is passed to a function. The function fails. The pointer is still uninitialized. It is passed to free() that goes crazy.

What can we do about it? Initialize all variables on stack? That would be quite expensive in terms of CPU load. But sometimes security is more important than speed.

free() can be hardened to only recognize previously allocated addresses. That's less expensive. But it's not a full solution. An attacker could manipulate the stack to contain an allocated pointer. Valid memory would be deallocated and overwritten with some other data.

Should we require that no variables are conditionally initialized? How can it be implemented? Perhaps a compile warning could be introduced. That would work on the callee side. But there should be a way for the compiler to know that the variable is potentially uninitialized, i.e. that it's an output variable.

I don't see what can be done on the caller side. Passing a pointed to uninitialized data would not be a problem. Neither would be the expectation that the data is initialized after the call.

Pretending that C is good enough if used carefully is not going to help.

Remote code execution vulnerability in Samba

Posted Feb 23, 2015 22:45 UTC (Mon) by abatters (✭ supporter ✭, #6932) [Link]

Here is another problem scenario that I have seen elsewhere: a pointer initialized to NULL is passed to a function. The function allocates memory and sets the pointer, but then something else fails, so the function free()s the memory and returns without setting the pointer back to NULL. The caller free()s the pointer also, leading to a double-free.

references:
http://marc.info/?l=linux-netdev&m=119153598401841&...
https://git.kernel.org/cgit/linux/kernel/git/torvalds/lin...

Remote code execution vulnerability in Samba

Posted Feb 23, 2015 23:37 UTC (Mon) by eternaleye (guest, #67051) [Link] (5 responses)

Should we require that no variables are conditionally initialized? How can it be implemented? Perhaps a compile warning could be introduced. That would work on the callee side. But there should be a way for the compiler to know that the variable is potentially uninitialized, i.e. that it's an output variable.

Well, this is solvable in other languages - case in point, Rust.

  1. No variables are conditionally initialized? Check.
  2. Output variables made less necessary by a native tuple return? Check.
  3. Pass in a preallocated buffer so the callee doesn't need to allocate, without permitting uninitialized reads? Check (Vec::with_capacity()). Move it into the function, and return it with tuple return. (or better yet, return a Result<Vec,Error>, which will free the Vec via RAII on error.)

Remote code execution vulnerability in Samba

Posted Feb 24, 2015 2:34 UTC (Tue) by idupree (guest, #71169) [Link] (4 responses)

I suspect this could be done even in C. Add a compile flag that initializes all stack memory the compiler can't prove is written before it's read, and link to a malloc/realloc that zeroes its memory. Then measure the performance hit. This wouldn't catch all uninitialized access and wouldn't have the performance of a language/library designed to work this way. But it could be an option for the vast amount of existing C code.

Remote code execution vulnerability in Samba

Posted Feb 24, 2015 5:35 UTC (Tue) by mathstuf (subscriber, #69389) [Link]

Doesn't UBSan do this already? Or at least one of the sanitizers.

Remote code execution vulnerability in Samba

Posted Feb 24, 2015 17:11 UTC (Tue) by zlynx (guest, #2285) [Link] (2 responses)

Ouch on the zero-init malloc.

I have profiler evidence that made me change from using strncpy to strlcpy exactly because zero-filling a 8kB URL buffer was using over 6% of the execution time.

Remote code execution vulnerability in Samba

Posted Feb 25, 2015 1:30 UTC (Wed) by idupree (guest, #71169) [Link] (1 responses)

Indeed, as a developer 6% is rather a lot. As a user, I would and have chosen 6% CPU time hits in the name of security, and scarcely noticed the difference. Not really sure how to reconcile these two perspectives :)

Remote code execution vulnerability in Samba

Posted Feb 25, 2015 15:41 UTC (Wed) by nybble41 (subscriber, #55106) [Link]

> As a user, I would and have chosen 6% CPU time hits in the name of security, and scarcely noticed the difference.

This is easy to say, but in practice most of the time you'd be taking a 6% performance hit with *no* improvement in security (because the data was already being initialized, or had no security impact), leading your users to start asking awkward questions, such as why your software seems so much slower then your competitors' with no improvement in the number of CVEs.

Remote code execution vulnerability in Samba

Posted Feb 23, 2015 23:53 UTC (Mon) by jra (subscriber, #55261) [Link] (36 responses)

I would like to change our development process to flag and fail to compile with uninitialized stack variables.

This was worse than uninitialized data passed to free, it was passed to talloc_free() which calls a destructor function. If the attacker can set up the stack in advance this can be made to call existing code.

It was a very hard one to exploit I think (we didn't take the time to do it), but not impossible, which was why it was flagged the way it was.

Remote code execution vulnerability in Samba

Posted Feb 24, 2015 10:43 UTC (Tue) by jezuch (subscriber, #52988) [Link]

> I would like to change our development process to flag and fail to compile with uninitialized stack variables.

In Java it's a compilation error to use a local variable that is not initialized in all paths leading to its use. This does not apply to non-final fields, though they are always initialized - to zero or null if not explicitly assigned. Very simple yet very effective. But Java is not C and the culture is a bit different and there are different idioms, like the one of passing a pointer as an output parameter that is almost nonexistent in Java world but very common in C. People would complain *a lot* if that was disallowed or even generated a warning :)

Remote code execution vulnerability in Samba

Posted Feb 24, 2015 11:15 UTC (Tue) by Cyberax (✭ supporter ✭, #52523) [Link] (34 responses)

> I would like to change our development process to flag and fail to compile with uninitialized stack variables.
Except that it might make matters worse.

Valgrind helps to find usages of uninitialized variables and this is often very helpful for actual debugging.

Remote code execution vulnerability in Samba

Posted Feb 24, 2015 16:48 UTC (Tue) by jra (subscriber, #55261) [Link] (33 responses)

We use valgrind in our testing.

This was in an error condition path that was very unlikely, so not exercised by normal valgrind testing.

It just so happened that a remote user could trigger this error path, which is why we have a regression test for this now that causes this path to be taken.

We do try to follow best practices for C development. Problem is, it's impossible to get right 100% of the time.

If I were doing Samba again from scratch, I'd do it in Google go.

Remote code execution vulnerability in Samba

Posted Feb 24, 2015 17:53 UTC (Tue) by nye (guest, #51576) [Link] (30 responses)

>If I were doing Samba again from scratch, I'd do it in Google go.

Is this just an offhand glib comment, or do you mean it literally?

If that latter, I'm curious why it would be Go that you'd pick specifically, because at first glance it seems like Rust would be a better fit for Samba.

Remote code execution vulnerability in Samba

Posted Feb 24, 2015 18:00 UTC (Tue) by Cyberax (✭ supporter ✭, #52523) [Link] (8 responses)

I'm trying to rewrite my C++ code in Rust and... so far I don't actually like it. My list of annoyances:
1) Unsafe code is required for a lot of stuff.

2) Too little object oriented stuff - there are trait objects, but they can't be cast from one type to another (i.e. dynamic_cast in C++). Sometimes it's needed.

3) COMPILATION SPEED. Or lack thereof.

4) Braindead exception handling. You have to be careful in your unsafe code to handle panics, yet panics themselves are useless as you can't recover from them. Long time ago with lightweight tasks and Erlang-style concurrency that made sense, but not now.

Remote code execution vulnerability in Samba

Posted Feb 25, 2015 5:04 UTC (Wed) by paulj (subscriber, #341) [Link] (7 responses)

One issue I have with Rust as a standard system language is that it's hard to call from outside Rust. It doesn't seem to be easily possible to write system libraries in Rust, AFAICT (the Rust runtime doesn't get setup fully, so without great care, the Rust code can easily trip up on that and blow up).

If you can't write standard system libraries in it, it surely is not ready as a system language.

I've switched to reading up on OCaml since. A fairly nice functional language, but not ridiculously strict about it as Haskell is, instead retaining some flexibility about things. So interfacing with state can be done more naturally. It has a rich body of existing modules. Crucially for me, it seems like it's much happier to be called into from non-OCaml code. It seems like you can make C-callable libraries from OCaml, relatively easily and without too many pitfalls.

I've been hunting around for a good language that would be suitable for integrating into existing C code, and allow some functions to be done in a safer way. So far Lua and OCaml seem the best things out there.

Remote code execution vulnerability in Samba

Posted Feb 25, 2015 5:06 UTC (Wed) by paulj (subscriber, #341) [Link]

Oh, that's "hunting around" in a reading up sense - I've not yet really tried anything out. Be glad to hear of experiences.

Remote code execution vulnerability in Samba

Posted Feb 25, 2015 6:23 UTC (Wed) by adler187 (guest, #80400) [Link] (1 responses)

FYI, the Rust runtime was removed recently and you can now call from C -> Rust without setup: https://github.com/rust-lang/rust/pull/19654

Remote code execution vulnerability in Samba

Posted Feb 25, 2015 8:03 UTC (Wed) by paulj (subscriber, #341) [Link]

That's very interesting. I'll have to look at Rust again. Thanks!

Remote code execution vulnerability in Samba

Posted Feb 25, 2015 6:29 UTC (Wed) by Cyberax (✭ supporter ✭, #52523) [Link] (2 responses)

These days Rust can be used to write bare-metal code with no runtime at all (and people have actually done just that: https://github.com/hackndev/zinc ). As a result, calling from C into Rust and back is easy and fast.

This also add a bit of idiosyncrasies - you have to support unwinding, but it's mostly useless now without lightweight tasks.

OCaml is interesting, but it's hard to integrate it with C-libraries. For example, it uses some one or two bits in integer variables for GC tagging.

Remote code execution vulnerability in Samba

Posted Feb 25, 2015 8:33 UTC (Wed) by paulj (subscriber, #341) [Link] (1 responses)

Will look at Rust again.

Reading the OCaml C interfaces chapter, I got the impression that it was possible to marshal OCaml types to/from C relatively easily? Do the tags really hinder much? I wouldn't really want C callers to be digging into the internals of library data structures, whether that library was written in OCaml or not. Or is there a greater issue?

E.g. http://caml.inria.fr/pub/docs/manual-ocaml-4.02/intfc.htm... - that and the rest of the chapter seem to suggest everything is there to allow a library to be implemented largely in OCaml, with a little C shim around it to export a regular C interface??

Remote code execution vulnerability in Samba

Posted Feb 25, 2015 10:43 UTC (Wed) by Cyberax (✭ supporter ✭, #52523) [Link]

> E.g. http://caml.inria.fr/pub/docs/manual-ocaml-4.02/intfc.htm... - that and the rest of the chapter seem to suggest everything is there to allow a library to be implemented largely in OCaml, with a little C shim around it to export a regular C interface??
Not really. OCaml relies on GC so anything that crosses the FFI boundary has to be extremely careful not to create cycles of unmanaged->managed->unamanaged objects. Which is surprisingly easy to do if you have a non-trivial library.

And yes, tagging bits are really annoying.

Remote code execution vulnerability in Samba

Posted Feb 27, 2015 13:45 UTC (Fri) by Kensan (guest, #52930) [Link]

There is also Ada [1] which is used for many safety and high integrity projects in Avionics, Aerospace, etc. If you want to go further and add formal proofs there is SPARK [2]. The latest standard is Ada 2012 [3] on which SPARK 2014 [4] is based.

Ada is a statically typed, compiled language and it's speed is comparable to C, see also "The Computer Language Benchmarks Game" [5]. It is a general purpose programming language that among other things is also well suited for low-level/embedded development [6] (Full disclosure: I am one of the authors of the Muen Separation Kernel).

The Ada compiler GNAT is part of GCC and packages for many Linux distros and BSDs are readily available [7].

There are many resources, libraries etc available online [8][9] and even though the Ada language is not well known, the community is quite active, e.g. the Ada developer room [10] has been part of FOSDEM since 2005.

[1] - http://en.wikipedia.org/wiki/Ada_%28programming_language%29
[2] - http://en.wikipedia.org/wiki/SPARK_%28programming_languag...
[3] - http://www.ada2012.org/
[4] - http://www.spark-2014.org/about
[5] - http://benchmarksgame.alioth.debian.org/
[6] - http://muen.sk/
[7] - http://getadanow.com/
[8] - http://www.adaic.org/ada-resources/
[9] - http://en.wikibooks.org/wiki/Ada_Programming
[10] - https://fosdem.org/2015/schedule/track/ada/

Remote code execution vulnerability in Samba

Posted Feb 24, 2015 19:49 UTC (Tue) by jra (subscriber, #55261) [Link] (19 responses)

Kind of an offhand comment, we don't have the engineers to rewrite 20+ years of C code in another language :-), plus that was tried with the original Samba4 codebase and we all know how that turned out (longer lead time than Duke Nukem Forever :-).

I don't know enough about Rust to make an intelligent comment, but go seems to be low-level enough it'd make a good fit for a Samba-like project.

Remote code execution vulnerability in Samba

Posted Feb 24, 2015 20:02 UTC (Tue) by Wol (subscriber, #4433) [Link] (3 responses)

The other language I would consider would be Modula-2. Caveat - I don't actually know much about it!

But I believe it was "Pascal for the real world", namely BY DEFAULT it had all of Pascal's insane "force the programmer to do it right" attitude, but it also accepted that theory and reality often collide painfully, and allowed the programmer to say "but I *need* to do this".

Cheers,
Wol

Modula-2

Posted Feb 24, 2015 21:50 UTC (Tue) by vonbrand (guest, #4458) [Link] (2 responses)

I had a brief run-in with Modula-2 way back around '86. It only has modules (essentially stand-alone objects, no classes and no inheritance). As it follows Pascal's type system, there are no variable-argument functions/procedures at all (not even predefined ones). Some safety hatches to get down to bit twiddling are available. Nice, but falls far short for my taste.

Modula-3

Posted Feb 27, 2015 5:06 UTC (Fri) by CChittleborough (subscriber, #60775) [Link] (1 responses)

No, Modula-2 is not useful for larger systems. But Modula-3 is.

With Wirth's blessing, various people designed and implemented successors to Modula-2, culminating in Modula-3. Several research projects used it, including an extensible OS, so it is suitable for low-level systems programming. The language spec is 80-odd pages long, reflecting the high priority the designers placed on simplicity, but it has classes with (single inheritance).

OTOH, the last release of a M3 compiler/linker/etc was CM3 5.8.6 in 2010. Sigh.

Modula-3

Posted Feb 27, 2015 10:29 UTC (Fri) by viro (subscriber, #7872) [Link]

I really wonder what had inspired them to do that kind of bootstrap horrors, especially since the damn thing was gcc-based anyway. With bits and pieces in m3, and _really_ unpleasant to bootstrap. For a while it had stayed around as "that weird build-dep of cvsup", but that had ended with (a) cvsup getting a replacement in C and (b) FreeBSD ditching CVS for their tree.

Oh, and they had recurring headaches inflicted by gcc evolution, as well. Hell knows, maybe LLVM-based implementation could have survived better, but their attitude regarding bootstrap vs. self-hosting would've been bloody painful no matter what...

Remote code execution vulnerability in Samba

Posted Feb 26, 2015 5:45 UTC (Thu) by ab (subscriber, #788) [Link] (13 responses)

Rust is just much better than Go in the current quest for system level languages. Sorry Jeremy, but unless Go solves for real dynamic library and executable story it is not a system level language yet.

Remote code execution vulnerability in Samba

Posted Feb 26, 2015 6:11 UTC (Thu) by Cyberax (✭ supporter ✭, #52523) [Link] (12 responses)

Samba is not a system-level program. It's a regular server that can and should be written in a regular language.

And static linking actually really helps with Go in practice. It's strange at first but then quickly becomes awesome.

Remote code execution vulnerability in Samba

Posted Feb 26, 2015 10:30 UTC (Thu) by ab (subscriber, #788) [Link] (4 responses)

Static linking does not help for system-wide code reuse. Getting more and more code duplicated within applications' address spaces isn't going to move us forward, security or otherwise.

Remote code execution vulnerability in Samba

Posted Feb 26, 2015 18:19 UTC (Thu) by Cyberax (✭ supporter ✭, #52523) [Link] (3 responses)

Go libraries allow better and easier code reuse than C libraries. They don't allow _runtime_ code deduplication but it turns out to be a pretty minor thing.

Remote code execution vulnerability in Samba

Posted Feb 27, 2015 4:20 UTC (Fri) by ab (subscriber, #788) [Link] (1 responses)

What is seen as a minor issue to you is a major issue in case of integrated solutions like distributions or actual NAS systems. It is not about disk space but rather about maintenance costs and actual memory pressure from non-deduplicated processes running on a device. Linux these days is pretty good with deduplication for dynamically linked ELF objects but this is simply not true for stuff like a typical Go application.

Security fixes for applications that are statically linked also go up in costs of maintenance. Where, say, Google thinks one can simply rebuild a container image and redeploy zillions of them because bandwidth costs of doing that are negligible in the grander scheme of things, doing the same for other environments is hardly justifiable. The world is flat but not equal yet.

Remote code execution vulnerability in Samba

Posted Feb 27, 2015 4:43 UTC (Fri) by Cyberax (✭ supporter ✭, #52523) [Link]

A typical NAS will run only one big process, so there's no problem with that. And larger devices have gigs of RAM and so a little of duplication is not that bad.

So dynamic libraries are a nice feature to have, but it's apparently not a deal breaker for most folks.

Remote code execution vulnerability in Samba

Posted Mar 6, 2015 14:04 UTC (Fri) by mathstuf (subscriber, #69389) [Link]

Depends. I remember someone complaining that Go executables were each (or maybe a collection of a few?) ~50M for some project which greatly inflates container/cloud deployment sizes (the binaries were from kubernetes). If you spin up a large number of machines, that extra disk can have real costs (rather than "disk is cheap").

Remote code execution vulnerability in Samba

Posted Feb 26, 2015 13:49 UTC (Thu) by nye (guest, #51576) [Link] (6 responses)

>Samba is not a system-level program. It's a regular server that can and should be written in a regular language.

So long as that regular language is a high-performance one, that is, which does rule out rather a lot of very high level languages.

This whole thread has been very interesting to me, especially discussions over whether, for example, a 6% CPU overhead is worth it. For most programs I'd say pretty well always, since most programs are never CPU bound and 6% of very little is very very little. Hell, a lot of programs have such trifling performance requirements that you can get away with writing them in *Python*. Samba on the other hand is always CPU bound for me because I tend to have weak CPUs on my NAS machines (and I can't imagine this is all that uncommon).

In my testing, I've been able to get bulk transfer rates from the kernel NFS server at around 5x what Samba can manage on the same system, with Illumos's in-kernel CIFS server sitting somewhere between the two (but I've not benchmarked this on the same system so it's harder to get good results). Samba seems to me therefore to be amongst the software most needing a high-performance programming language, outside of OS kernels and games.

Remote code execution vulnerability in Samba

Posted Feb 26, 2015 14:52 UTC (Thu) by cesarb (subscriber, #6266) [Link] (2 responses)

> In my testing, I've been able to get bulk transfer rates from the kernel NFS server at around 5x what Samba can manage on the same system, with Illumos's in-kernel CIFS server sitting somewhere between the two

Was the transfer from the NFS server encrypted? It's possible that Samba was using authenticated encryption while the other two weren't.

Remote code execution vulnerability in Samba

Posted Feb 26, 2015 15:04 UTC (Thu) by madscientist (subscriber, #16861) [Link]

I think nye's point was not that NFS is faster than Samba, but that Samba is performance-critical enough that a slowdown of even a few percent will absolutely be noticeable.

Remote code execution vulnerability in Samba

Posted Feb 26, 2015 15:17 UTC (Thu) by nye (guest, #51576) [Link]

>Was the transfer from the NFS server encrypted? It's possible that Samba was using authenticated encryption while the other two weren't.

Honestly I don't recall any more, since it's been a couple of years. The Illumos test was though.

But as madscientist pointed out, the point isn't so much about the absolute performance numbers, but that Samba is particularly performance-sensitive compared to most software, where squeezing a few more percent by writing in lower level languages isn't typically worth the cost in terms of code complexity/security difficulties/etc.

Remote code execution vulnerability in Samba

Posted Feb 26, 2015 17:20 UTC (Thu) by jra (subscriber, #55261) [Link] (2 responses)

> In my testing, I've been able to get bulk transfer rates from the kernel
> NFS server at around 5x what Samba can manage on the same system

Ah, but this can *massively* depend on the client.

Using smbclient to smbd we can usually outperform any other client for bulk data transfer, as smbclient was rewritten by Volker to drive SMB1 (and now SMB2) to the limits of what the protocol allows, by pipelining reads and writes. We can saturate most network links using our own code.

The Windows redirector client just doesn't use the protocol as agressively as we do. Steve or Jeff can comment on the Linux CIFSFS kernel client if they wish.

Remote code execution vulnerability in Samba

Posted Feb 26, 2015 18:04 UTC (Thu) by zlynx (guest, #2285) [Link]

Windows 7 and 8 seem to use SMB / CIFS much better than older Windows versions. My 8.1 desktop can easily get 80 MB/s downloading from my NAS. The NAS is an AMD E350 system with two WD Reds running Fedora 21.

Remote code execution vulnerability in Samba

Posted Feb 28, 2015 8:59 UTC (Sat) by riking (guest, #95706) [Link]

> by pipelining reads and writes

Sounds like what Go was made to do :)

Remote code execution vulnerability in Samba

Posted Feb 28, 2015 8:57 UTC (Sat) by riking (guest, #95706) [Link]

Actually, it's not whether or not Go is "low level" that makes it a good fit for Samba. It's the fact that Samba is a networking daemon that makes it a good fit with Go.

Go was written by and for people that needed to handle network connections, and it shows, both in the features and the stdlib.

Remote code execution vulnerability in Samba

Posted Feb 25, 2015 11:25 UTC (Wed) by nye (guest, #51576) [Link]

Interesting responses all, thanks.

(One of these days I will get around to learning Rust, I swear.)

Remote code execution vulnerability in Samba

Posted Mar 12, 2015 11:24 UTC (Thu) by jwakely (guest, #60262) [Link] (1 responses)

> If I were doing Samba again from scratch, I'd do it in Google go.

A fine idea,. If you don't ever need to use a debugger. Or compile for anything except x86 and ARM (using the official toolchain anyway).

Remote code execution vulnerability in Samba

Posted Mar 15, 2015 6:41 UTC (Sun) by lsl (subscriber, #86508) [Link]

Power and ARM64 are almost there for the gc toolchain, with people working on S/390. As you probably know, there's also gccgo which isn't any less official than the ones based on the Plan 9 compilers.

Of course, the Go ecosystem isn't anywhere as mature as what we have for C. But that's somewhat expected for a (comparatively) young language, isn't it?

Remote code execution vulnerability in Samba

Posted Feb 24, 2015 6:42 UTC (Tue) by eru (subscriber, #2753) [Link] (12 responses)

Initialize all variables on stack? That would be quite expensive in terms of CPU load.

Automatic zero-initializing of all scalar stack variables would cost very little or nothing, because the compiler can optimize away most of the initializations.

Remote code execution vulnerability in Samba

Posted Feb 24, 2015 8:47 UTC (Tue) by ibukanov (subscriber, #3942) [Link] (5 responses)

Indeed, C/C++ compilers should by default zero all unitalicized variables that they cannot proof will be written before a read unless a special flag --i-dont-care-about-security is passed...

Any performance savings from not doing this in the long term is nothing compared with the cost of dealing with exploits. It is like selling naked options. It looks attractive and provides steady income in a short term. But then unexpected event comes and wipes out all the gains and brings a heavy debt burden...

Remote code execution vulnerability in Samba

Posted Feb 24, 2015 9:04 UTC (Tue) by PaXTeam (guest, #24616) [Link]

the structleak plugin in PaX can be adopted to this already. it was originally written to handle a particular type of uninitialized variable problem in linux (CVE-2013-2141) but it can be extended to cover all uninitialized local variables (and not just structure types as it does now).

Remote code execution vulnerability in Samba

Posted Feb 24, 2015 15:59 UTC (Tue) by welinder (guest, #4699) [Link] (3 responses)

Zero-initializing is a bad idea because someone will start relying
on it and before you know the same code will be used elsewhere
where there is no zeroing. 0xdeadbeef sounds a lot better.
But even that isn't the way to go.

-fsanitize=undefined ought to catch this and terminate the program.
This should be cheap enough that most programs can be compiled with
the protection on. People running benchmark could turn it off as
needed.

Remote code execution vulnerability in Samba

Posted Feb 24, 2015 16:55 UTC (Tue) by ibukanov (subscriber, #3942) [Link] (1 responses)

Crashing on uninitialized access would be nice, but how it could be implemented? For example, how a C compiler can deduce that the following should terminate the program:

int foo(int *bar) {
return *bar;
}

int test;
foo(&test);

when foo lives in a shared library and no whole program analysis is possible?

So it sounds like that the most pragmatic solution is to require by default that one can get a pointer only for already initialized things unless the compiler can prove that &uninitialized is safe. This should be compatible with older compilers.

Remote code execution vulnerability in Samba

Posted Feb 25, 2015 13:34 UTC (Wed) by mathstuf (subscriber, #69389) [Link]

UBSan does that. Basically, it tools the code to keep some bits for each memory location and all accesses check the map for whether it is initialized or not. It isn't free, but it is oh-so-useful.

Remote code execution vulnerability in Samba

Posted Feb 26, 2015 0:12 UTC (Thu) by nlewycky (guest, #63373) [Link]

-fsanitize=undefined does not catch use of uninitialized values. You want -fsanitize=memory for that. http://clang.llvm.org/docs/MemorySanitizer.html (Mind you, -fsanitize=bool is something of a cheap msan, but we don't deliberately initialize bool objects to a non-true/non-false value.)

Clang's -Wuninitialized and -Wsometimes-uninitialized will probably do what you actually want in a warning. The "sometimes uninitialized" warning works by assuming that if you have an if-statement then you intend for it to potentially be false sometimes or true sometimes, and therefore that the value must be initialized either way (similarly for other control flow constructs), however if it can't show otherwise then it will assume that you have arranged multiple variables to be correlated and suppresses the warning if it's a false positive. (Note: passing an uninitialized value as a parameter has undefined behaviour before the callee ever runs.)

You may think that you want it to warn more aggressively, but then I suggest you try clang or gcc's -Wconditional-uninitialized warning. It's useless because it fires everywhere. -Wsometimes-uninitialized can be used with -Werror.

Remote code execution vulnerability in Samba

Posted Feb 24, 2015 10:52 UTC (Tue) by jezuch (subscriber, #52988) [Link] (5 responses)

> Automatic zero-initializing of all scalar stack variables would cost very little or nothing

...and it would be the wrong thing to do. I see a lot of "fixes" to the warning about use of an uninitialized variable that just assign 0 to the variable it its definition. This is not a fix. The fix is to find the path that leaves the variable uninitialized and either initialize it there or exit the loop/function/whatever so that the variable is not used later on at all. IOW: you have to know what you're doing and automatic initialization of local variables does not help with that at all (actually I think it's actively harmful because it disables useful warnings).

Remote code execution vulnerability in Samba

Posted Feb 24, 2015 12:35 UTC (Tue) by eru (subscriber, #2753) [Link] (1 responses)

...and it would be the wrong thing to do.

This is a theory vs practice issue. In principle, I agree with you. In practice (and seeing the ghastly messes people working under schedule pressures and with less than stellar C knowledge can produce), I think anything that makes the inevitable errors less dangerous and less random (= easier to reproduce) is good.

Remote code execution vulnerability in Samba

Posted Feb 24, 2015 13:23 UTC (Tue) by cesarb (subscriber, #6266) [Link]

Why not both? Since it's undefined behavior, a compiler can zero-initialize uninitialized variables while at the same time producing a compile-time warning.

Remote code execution vulnerability in Samba

Posted Feb 24, 2015 13:22 UTC (Tue) by cesarb (subscriber, #6266) [Link] (2 responses)

> I see a lot of "fixes" to the warning about use of an uninitialized variable that just assign 0 to the variable it its definition. This is not a fix. The fix is to find the path that leaves the variable uninitialized and either initialize it there or exit the loop/function/whatever so that the variable is not used later on at all.

What if all the paths that use the variable initialize it, but the compiler can't see it? For instance, if the variable is only used if a complicated condition is true, and earlier in the function initialized only if a different complicated condition (which is a superset of the other condition) is true? In that case, a dummy initialization to a "neutral" value both silences the compiler warning and assures the readers there's no chance of it being uninitialized. If the compiler ever becomes smart enough to notice that the initialization is redundant, it will optimize out the assignment.

OTOH, a useful idiom is to only declare the variables at their point of initialization; not always possible, but neatly sidesteps the whole issue (while at the same time annoying Windows developers, since last I looked their default compiler still didn't allow for mixed statements and declarations, a 15-year-old feature of the C language).

Remote code execution vulnerability in Samba

Posted Feb 24, 2015 14:16 UTC (Tue) by rodrigorc (guest, #89475) [Link]

> ...while at the same time annoying Windows developers, since last I looked their default compiler still didn't allow for mixed statements and declarations, a 15-year-old feature of the C language.

I happen to have a few MS-VC compilers around... A quick check with:

int main() { 0; int class; }

(the variable is named `class` to prevent accidental compiling as C++).

VC-2010 (CL version 16): error C2143: syntax error : missing ';' before 'type'
VC-2012 (CL version 18): OK!

So they finally did it, it just took 12 years.

Remote code execution vulnerability in Samba

Posted Feb 25, 2015 9:19 UTC (Wed) by jezuch (subscriber, #52988) [Link]

> What if all the paths that use the variable initialize it, but the compiler can't see it? For instance, if the variable is only used if a complicated condition is true, and earlier in the function initialized only if a different complicated condition (which is a superset of the other condition) is true?

Then you have control flow with complex variable interdependencies that is confusing not only to the compiler but the more so to all the human readers of your code. Refactor! ;) But I know what you're talking about. I see this pattern a lot with null pointer analysis done by some tools for Java. I always try to take these warnings seriously even if the code is in fact correct, if only because getting rid of it (and not by simply silencing it) would make the code clearer.

Remote code execution vulnerability in Samba

Posted Feb 24, 2015 11:25 UTC (Tue) by epa (subscriber, #39769) [Link] (9 responses)

Yes, I don't understand why the default behaviour of free() is so dangerous. Surely free() should only work to free something already malloc()ed, and just kaboom the process otherwise (which is much less dangerous than the memory corruption it currently does).

Of course, this extra checking would slow things down. So you could have a free_unsafe() for time-critical where the programmer has chosen to take the risk. But 'unsafe by default' cannot be a good idea. The four-letter function has to be the safe one, and the more dangerous operation the more verbose code.

Remote code execution vulnerability in Samba

Posted Feb 24, 2015 13:34 UTC (Tue) by cesarb (subscriber, #6266) [Link] (6 responses)

> Yes, I don't understand why the default behaviour of free() is so dangerous. Surely free() should only work to free something already malloc()ed, and just kaboom the process otherwise (which is much less dangerous than the memory corruption it currently does).

How could free() know whether something came from malloc() or not? It receives a pointer, and somehow has to extract from it control information (including but not limited to the size of the allocated block). Usually, that's placed just before the block. If you pass free() a pointer which didn't come from malloc(), it will find garbage where it expects to see the control information.

> Of course, this extra checking would slow things down. So you could have a free_unsafe() for time-critical where the programmer has chosen to take the risk. But 'unsafe by default' cannot be a good idea. The four-letter function has to be the safe one, and the more dangerous operation the more verbose code.

The way to make free() safer would be to have, in a separate place, a list of allocated blocks, and look in that list for the passed pointer (instead of trusting that the information can always be found right next to it). But that means that malloc() would take most of the extra cost, not free(); you'd need a malloc_unsafe() together with the free_unsafe().

Remote code execution vulnerability in Samba

Posted Feb 24, 2015 13:56 UTC (Tue) by epa (subscriber, #39769) [Link] (5 responses)

You're right, the extra cost is mostly imposed on malloc() so it is that function that would need an 'unsafe' variant too. I still think that more safety by default would help everyone.

Remote code execution vulnerability in Samba

Posted Feb 24, 2015 17:25 UTC (Tue) by reedstrm (guest, #8467) [Link]

A code base in C that does all this wrapping malloc stuff is postgresql, with palloc and pfree. This is actually a hierarchical memory context allocator, to allow for allocating storage for the right lifetime, primarily targeted at avoiding memory leaks. It uses the 'allocation header just above the allocated memory' style system mentioned above, and does what it can to validate that an arbitrary pointer passed to it is in fact something allocated by palloc. Of course, pfree() itself mostly just makes that memory available to be reused in its allocation context pool: it's not freed to the system until that context is destroyed.

Remote code execution vulnerability in Samba

Posted Feb 25, 2015 10:45 UTC (Wed) by etienne (guest, #25256) [Link] (3 responses)

> [malloc] I still think that more safety by default would help everyone.

Seems that you already have it for a long time, you just did not even try to read the right manual, and never tried setting the variable MALLOC_CHECK_ in your .bashrc because it takes too much CPU time:

man 3 malloc
...
Recent versions of Linux libc (later than 5.4.23) and glibc (2.x) include a malloc() implementation which is tunable via environment variables. When MALLOC_CHECK_ is set, a special (less efficient) implementation is used which is designed to be tolerant against simple errors, such as double calls of free() with the same argument, or overruns of a single byte (off-by-one bugs). Not all such errors can be protected against, however, and memory leaks can result.
If MALLOC_CHECK_ is set to 0, any detected heap corruption is silently ignored; if set to 1, a diagnostic message is printed on stderr; if set to 2, abort(3) is called immediately; if set to 3, a diagnostic message is printed on stderr and the program is aborted.

Obviously, you also do not want to see the warnings so you do not keep stderr open/visible - same you do not keep an xconsole open when running X neither.

The C language is made for people who know what they do, if a variable is not initialised it is because it shall not be initialised. If you modify a function in C you have to understand the whole function first; if your way to program is to modify random lines and see with a debugger if your own test case works then you'd better use another language.

Remote code execution vulnerability in Samba

Posted Feb 25, 2015 11:19 UTC (Wed) by cesarb (subscriber, #6266) [Link]

> The C language is made for people who know what they do

Even people who know what they do make mistakes. Making it harder for mistakes to happen is a good thing.

Remote code execution vulnerability in Samba

Posted Feb 25, 2015 12:37 UTC (Wed) by epa (subscriber, #39769) [Link] (1 responses)

My point is not that safer ways of doing it do not exist, but that it makes more sense for the safe way to be the default, with the faster but unsafe way turned on for those who know what they are doing.

In this case, Samba is clearly written by "people who know what they do", and yet they still made a mistake.

Remote code execution vulnerability in Samba

Posted Feb 25, 2015 13:04 UTC (Wed) by vonbrand (guest, #4458) [Link]

"Fast, but unsafe" will be selected more often than not by the clueless...

Remote code execution vulnerability in Samba

Posted Feb 24, 2015 16:46 UTC (Tue) by jra (subscriber, #55261) [Link] (1 responses)

It wasn't free. It was talloc_free(). talloc_free() calls a desctuctor.

So if you can set up a bogus talloc_object when freed it will call a function that you can specify if you know the address of it in the program text space.

This was not an uninitialized free() problem, it was worse.

Remote code execution vulnerability in Samba

Posted Feb 25, 2015 12:39 UTC (Wed) by epa (subscriber, #39769) [Link]

Ouch, thanks for the correction.


Copyright © 2015, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds