Jump to page: 1 2 3
Thread overview
Scenario: OpenSSL in D language, pros/cons
May 04, 2014
Daniele M.
May 04, 2014
Jonathan M Davis
May 04, 2014
Daniele M.
May 05, 2014
Jonathan M Davis
May 05, 2014
JR
May 05, 2014
Marc Schütz
May 05, 2014
Jonathan M Davis
May 05, 2014
Daniele M.
May 06, 2014
Timon Gehr
May 06, 2014
Jonathan M Davis
May 06, 2014
JR
May 04, 2014
Meta
May 04, 2014
Nick Sabalausky
May 04, 2014
Daniele M.
May 05, 2014
Jacob Carlborg
May 05, 2014
Jonathan M Davis
May 05, 2014
Paulo Pinto
May 05, 2014
Jonathan M Davis
May 05, 2014
Paulo Pinto
May 05, 2014
Etienne
May 05, 2014
Dmitry Olshansky
May 05, 2014
Daniele M.
May 05, 2014
Etienne
May 06, 2014
Rikki Cattermole
May 04, 2014
I have read this excellent article by David A. Wheeler:

http://www.dwheeler.com/essays/heartbleed.html

And since D language was not there, I mentioned it to him as a possible good candidate due to its static typing and related features.

However, now I am asking the community here: would a D implementation (with GC disabled) of OpenSSL have been free from Heartbleed-type vulnerabilities? Specifically http://cwe.mitre.org/data/definitions/126.html and http://cwe.mitre.org/data/definitions/20.html as David mentions.

I find this perspective very interesting, please advise :)
May 04, 2014
On Sun, 04 May 2014 08:34:19 +0000
"Daniele M. via Digitalmars-d" <digitalmars-d@puremagic.com> wrote:

> I have read this excellent article by David A. Wheeler:
>
> http://www.dwheeler.com/essays/heartbleed.html
>
> And since D language was not there, I mentioned it to him as a possible good candidate due to its static typing and related features.
>
> However, now I am asking the community here: would a D implementation (with GC disabled) of OpenSSL have been free from Heartbleed-type vulnerabilities? Specifically http://cwe.mitre.org/data/definitions/126.html and http://cwe.mitre.org/data/definitions/20.html as David mentions.
>
> I find this perspective very interesting, please advise :)

@safe code protects against indexing an array out-of-bounds. So, if OpenSSL had been implemented in D, and its heartbeat feature used @safe code, then heartbleed would not have been possible. As soon as an attempt was made to index passed the end of the array, it would have thrown a RangeError and killed the program.

Now, even if OpenSSL had been implemented in D, if it had used @system or @trusted code for its heartbeat feature, then it could have had the bug just as easily in D as it did in C. And given all of the horrible practices in OpenSSL, I very much doubt that having it written in D would have prevented much, because anyone making the choices that the OpenSSL folks have been making would likely have ended up with horrible D code which was mostly @system and probably doing all kinds of things that are inherently risky, forgoeing many of the benefits that D provides.

I think that it's safe to say that D makes it easier to catch problems like this, but it doesn't entirely prevent them, and bad programming practices can pretty much always get around protections that the language provides unless the language provides no ways around those protections (which isn't the case in D, because it's a systems language and needs to provide low-level access and features for those programs that need them - it just doesn't use those by default).

If I had more time, I'd actually be tempted to write an SSL implementation in D, but even if I were to do an excellent job of it, it would still need to be vetted by security experts to make sure that it didn't have horrible security bugs in it (much as it would be likely that there would be fewer thanks to the fact that it would be writen in D), and I suspect that it's the kind of thing that many people aren't likely to trust because of how critical it is. So, I don't know how good an idea it is at this point for someone to write an implementation of SSL or TLS in D. Certainly, it's the type of thing where we've generally tried to wrap existing C libraries in order to avoid having to spend the time, effort, and expertise on in order to fully implement it ourselves. The Go guys did it, but if I understand correctly, the fellow that did it was one of the OpenSSL developers, so presumably he's already very familiar with all of the ins and outs of SSL, and I don't know if any of us here are (I'm certainly not - if I were doing it, I'd pretty much just have to go off of its associated RFCs, for better or worse).

At this point though, if I were looking at using an existing implementation of SSL, I'd probably be looking at GnuTLS rather than OpenSSL given how horrible OpenSSL's codebase is. I don't know that GnuTLS is any better, but it wouldn't be hard for it to be. OpenSSL is horrible both with regards to its implementation and its API, and we'd all be better off if something better replaced it (be it GnuTLS or something else). Unfortunately, even if something better _were_ written in D, it's probably only the D folks who would benefit, since it's not terribly likely at this point that very many folks are going to wrap a D library in order to use it another language.

However, regardless of whether we ever end up with an SSL implementation in D, I think that in the long run, D will show itself to be much, much better than C or C++ at writing code that has a low number of security bugs.

- Jonathan M Davis
May 04, 2014
On Sunday, 4 May 2014 at 08:34:20 UTC, Daniele M. wrote:
> I have read this excellent article by David A. Wheeler:
>
> http://www.dwheeler.com/essays/heartbleed.html
>
> And since D language was not there, I mentioned it to him as a possible good candidate due to its static typing and related features.
>
> However, now I am asking the community here: would a D implementation (with GC disabled) of OpenSSL have been free from Heartbleed-type vulnerabilities? Specifically http://cwe.mitre.org/data/definitions/126.html and http://cwe.mitre.org/data/definitions/20.html as David mentions.
>
> I find this perspective very interesting, please advise :)

While D is a somewhat safer language by *default*, it makes it fairly easy to escape from the safe part of the language and write unsafe code (array bounds checking can be turned off even for @safe code). Seeing as the OpenSSL devs went as far as to write an a buggy, custom implementation of malloc for a speed gain, turning off array bounds checking and ignoring @safe seems like the first thing they would do. The only language I would really trust is one in which it is impossible to write unsafe code, because you can then know that the developers can't use such unsafe hacks, even if they wanted to.
May 04, 2014
On 5/4/2014 9:29 AM, Meta wrote:
>
> While D is a somewhat safer language by *default*, it makes it fairly
> easy to escape from the safe part of the language and write unsafe code

Yea, I'm finding that in some ways, D accidentally encourages @system/@trusted code. For example, if you need some sensitive data zeroed out when done, and for whatever reason you aren't able to just have it live on the stack (to use RAII), then you need RefCounted which AFAICS throws any chance of being @safe out the window.

Maybe RefCounted could somehow take advantage of Unique to provide @safe-ty?

May 04, 2014
On Sunday, 4 May 2014 at 10:23:38 UTC, Jonathan M Davis via Digitalmars-d wrote:
> On Sun, 04 May 2014 08:34:19 +0000
> "Daniele M. via Digitalmars-d" <digitalmars-d@puremagic.com> wrote:
>
>> I have read this excellent article by David A. Wheeler:
>>
>> http://www.dwheeler.com/essays/heartbleed.html
>>
>> And since D language was not there, I mentioned it to him as a
>> possible good candidate due to its static typing and related
>> features.
>>
>> However, now I am asking the community here: would a D
>> implementation (with GC disabled) of OpenSSL have been free from
>> Heartbleed-type vulnerabilities? Specifically
>> http://cwe.mitre.org/data/definitions/126.html and
>> http://cwe.mitre.org/data/definitions/20.html as David mentions.
>>
>> I find this perspective very interesting, please advise :)
>
> @safe code protects against indexing an array out-of-bounds. So, if OpenSSL
> had been implemented in D, and its heartbeat feature used @safe code, then
> heartbleed would not have been possible. As soon as an attempt was made to
> index passed the end of the array, it would have thrown a RangeError and
> killed the program.
>
This is what I thought too.

> Now, even if OpenSSL had been implemented in D, if it had used @system or
> @trusted code for its heartbeat feature, then it could have had the bug just
> as easily in D as it did in C. And given all of the horrible practices in
> OpenSSL, I very much doubt that having it written in D would have prevented
> much, because anyone making the choices that the OpenSSL folks have been
> making would likely have ended up with horrible D code which was mostly
> @system and probably doing all kinds of things that are inherently risky,
> forgoeing many of the benefits that D provides.
>
> I think that it's safe to say that D makes it easier to catch problems like
> this, but it doesn't entirely prevent them, and bad programming practices
> can pretty much always get around protections that the language provides
> unless the language provides no ways around those protections (which isn't
> the case in D, because it's a systems language and needs to provide low-level
> access and features for those programs that need them - it just doesn't use
> those by default).
>
I should have said: a D implementation of OpenSSL *without* the malloc sorcerery, sorry for leaving it out of OP. Reason I'd like to ignore that for this discussion is that I consider it a bug. And we are all in the post-heartbleed scenario now so it looks huge, but Theo De Raadt already said it all, nothing to add on it.

And then comes my next question: except for that malloc-hack, would it have been possible to write it in @safe D? I guess that if not, module(s) could have been made un-@safe. Not saying that a similar separation of concerns was not possible in OpenSSL itself, but that D could have made it less development-expensive in my opinion.

> If I had more time, I'd actually be tempted to write an SSL implementation in
> D, but even if I were to do an excellent job of it, it would still need to be
> vetted by security experts to make sure that it didn't have horrible security
> bugs in it (much as it would be likely that there would be fewer thanks to
> the fact that it would be writen in D), and I suspect that it's the kind of
> thing that many people aren't likely to trust because of how critical it is.
Nobody would expect/trust a single person to do this job :P
Working in an open source project would be best.

> So, I don't know how good an idea it is at this point for someone to write
> an implementation of SSL or TLS in D. Certainly, it's the type of thing where
> we've generally tried to wrap existing C libraries in order to avoid having
> to spend the time, effort, and expertise on in order to fully implement it
> ourselves. The Go guys did it, but if I understand correctly, the fellow that
> did it was one of the OpenSSL developers, so presumably he's already very
> familiar with all of the ins and outs of SSL, and I don't know if any of us
> here are (I'm certainly not - if I were doing it, I'd pretty much just have
> to go off of its associated RFCs, for better or worse).
>
I wasn't aware of this effort, interesting experiment.

> At this point though, if I were looking at using an existing implementation
> of SSL, I'd probably be looking at GnuTLS rather than OpenSSL given how
> horrible OpenSSL's codebase is. I don't know that GnuTLS is any better, but
> it wouldn't be hard for it to be. OpenSSL is horrible both with regards to
> its implementation and its API, and we'd all be better off if something
> better replaced it (be it GnuTLS or something else).
I would pick LibreSSL:
http://www.libressl.org/

And merge patches as their progress goes on.

> Unfortunately, even if
> something better _were_ written in D, it's probably only the D folks who
> would benefit, since it's not terribly likely at this point that very many
> folks are going to wrap a D library in order to use it another language.
>
Here I don't completely agree: if we can have a binary-compatible implementation done in D, then we would be able to modify software to eventually use it as a dependency. I don't see the necessary D dependencies as prohibitive here.

> However, regardless of whether we ever end up with an SSL implementation in D,
I started this discussion just for the thought exercise, because I think it can give a few positive leads, but nice to see that other people too thought about a possible implementation :)

> I think that in the long run, D will show itself to be much, much better than
> C or C++ at writing code that has a low number of security bugs.
>
> - Jonathan M Davis
Thanks for your post Jonathan, however I believe that attempts like Go's are what makes a language more real and less theoretical. The field test of D on system-level software would be in my opinion useful and sometimes I think the sooner the better, to eventually modify the evolution of this beautiful language (if there's still a viable window for this).
May 04, 2014
On Sunday, 4 May 2014 at 13:29:34 UTC, Meta wrote:
> On Sunday, 4 May 2014 at 08:34:20 UTC, Daniele M. wrote:
>> I have read this excellent article by David A. Wheeler:
>>
>> http://www.dwheeler.com/essays/heartbleed.html
>>
>> And since D language was not there, I mentioned it to him as a possible good candidate due to its static typing and related features.
>>
>> However, now I am asking the community here: would a D implementation (with GC disabled) of OpenSSL have been free from Heartbleed-type vulnerabilities? Specifically http://cwe.mitre.org/data/definitions/126.html and http://cwe.mitre.org/data/definitions/20.html as David mentions.
>>
>> I find this perspective very interesting, please advise :)
>
> While D is a somewhat safer language by *default*, it makes it fairly easy to escape from the safe part of the language and write unsafe code (array bounds checking can be turned off even for @safe code). Seeing as the OpenSSL devs went as far as to write an a buggy, custom implementation of malloc for a speed gain, turning off array bounds checking and ignoring @safe seems like the first thing they would do. The only language I would really trust is one in which it is impossible to write unsafe code, because you can then know that the developers can't use such unsafe hacks, even if they wanted to.

You are right, devs would eventually abuse everything possible, although it would make it for sure more visible: you cannot advertize an un-@safe library as @safe, although I agree that a lot depends from devs/users culture.
May 05, 2014
On Sun, 04 May 2014 21:18:22 +0000
"Daniele M. via Digitalmars-d" <digitalmars-d@puremagic.com> wrote:

> On Sunday, 4 May 2014 at 10:23:38 UTC, Jonathan M Davis via Digitalmars-d wrote:

> And then comes my next question: except for that malloc-hack, would it have been possible to write it in @safe D? I guess that if not, module(s) could have been made un-@safe. Not saying that a similar separation of concerns was not possible in OpenSSL itself, but that D could have made it less development-expensive in my opinion.

I don't know what all OpenSSL is/was doing, and I haven't looked into it in great detail. I'm familiar with what caused heartbleed, and I'm somewhat familiar with OpenSSL's API from having dealt with it at work, but most of what I know about OpenSSL, I know from co-workers who have had to deal with it and other stuff that I've read about it, and in general, from what I understand, it's just plain badly designed and badly written, and it's a miracle that it works as well as it does. Most of the problems seem to stem from how the project is managed (including having horrible coding style and generally not liking to merge patches), but it's also certain that a number of the choices that they've made make it easier for security problems to creep in (e.g. using their own malloc in an attempt to gain some speed on some OSes).

>From what I know of SSL itself (and I've read some of the spec, but not all of
it), very little of it (and probably none of it save for the actual operations on the sockets) actually requires anything that's @system. The problem is when you go to great lengths to optimize the code, which the OpenSSL guys seem to have done. When you do that, you do things like turn off array bounds checking and generally try and avoid many of the safety features that a language like D provides, since many of them do incur at least some overhead.

Actually implementing SSL itself wouldn't take all that long from what I understand. The main problem is in maintenance - probably in particular with regards to the fact that you'd have to keep adding support for more encryption methods as they come out (which technically aren't part of SSL itself), but I'm not familiar enough with the details to know all of the nooks and crannies that would cause maintenance nightmares. The base spec is less than 70 pages long though. The fellow who answered the question here seems to think that implementing SSL itself is actually fairly easy and that he's done it several times for companies already:

http://security.stackexchange.com/questions/55465

I fully expect that if someone were to implement it in D, it would be safer out of the box than a C implementation would be. But if you had to start playing tricks to get it faster, that would increase the security risk, and in order for folks to trust it, you'd have to get the code audited, which is a whole other order of pain (and one that potentially costs money, depending on who does the auditing).

> > If I had more time, I'd actually be tempted to write an SSL
> > implementation in
> > D, but even if I were to do an excellent job of it, it would
> > still need to be
> > vetted by security experts to make sure that it didn't have
> > horrible security
> > bugs in it (much as it would be likely that there would be
> > fewer thanks to
> > the fact that it would be writen in D), and I suspect that it's
> > the kind of
> > thing that many people aren't likely to trust because of how
> > critical it is.
> Nobody would expect/trust a single person to do this job :P Working in an open source project would be best.

If someone around here implemented SSL in D, I fully expect that it would be open source, and I fully expect that one person could do it. It's just a question of how long it would take them - though obviously sharing the work among multiple people could make it faster. Where it's definitely required that more people get involved is when you want the code audited to ensure that it's actually safe and secure. And that's where implementing an SSL library is fundamentally different from implementing most libraries - it's so integral to security that it doesn't really cut it to just throw an implementation together and toss it out there for folks to use it if they're interested.

> > Unfortunately, even if
> > something better _were_ written in D, it's probably only the D
> > folks who
> > would benefit, since it's not terribly likely at this point
> > that very many
> > folks are going to wrap a D library in order to use it another
> > language.
> >
> Here I don't completely agree: if we can have a binary-compatible implementation done in D, then we would be able to modify software to eventually use it as a dependency. I don't see the necessary D dependencies as prohibitive here.

If D were to be part of a typical Linux distro, a D compiler would have to be
part of a typical Linux distro. We're getting there with gdc as it's getting
merged into gcc, but I don't think that it's ended up on many distros yet, and
you then end up with the problem of compatabitily, because while D is far, far
more stable than it used to be, it's still pretty common that small things
change or break over time (sometimes by necessity, sometimes due to
regressions), so writing a D library that could be used on a Linx system
with the same compiler version for several years is probably still a bit
unrealistic (though we're getting closer). And without that, even with a solid
C wrapper around it, nothing that's normally part of a Linux distro is going
to use it. At most, you'd get the occasional, smaller project that used it.

And as for Windows, that's not terribly likely to work at this point, because we only use the MS linker for 64-bit programs, and it's still frequently the case that Windows programs are build for 32-bit, because the developrs don't want to have to require 64-bit due to all of the 32-bit copies of Windows that are still sold.

So, while it's theoretically feasible to have a D library wrapped in C and then used in C and C++ programs by folks outside the D community, I don't think that it's actually realistic at this point.

> Thanks for your post Jonathan, however I believe that attempts like Go's are what makes a language more real and less theoretical. The field test of D on system-level software would be in my opinion useful and sometimes I think the sooner the better, to eventually modify the evolution of this beautiful language (if there's still a viable window for this).

It's mostly an issue of time and man power. I have quite a few projects that I'd like to work on for myself and for the D community at large, and I only have the time to work on a fraction of them, if that. And the same goes for many of the rest of the folks around here. We tend to try and direct our time and energy to projects that are of greater benefit (which often means not reimplementing stuff that exists in C and can easily be wrapped - especially if reimplementing it in D would be time consuming), but regardless of where we spend our time and energy, we just plain need more people to pitch in if we want more stuff implemented in D.

Something like SSL is only going to be implemented in D if a member of the community decides that it's worth it to them to spend the time doing it, and they have the knowledge and expertise to pull it off. And given all of the other things that need to be done, I'm not sure that it even makes sense to spend time on implementing SSL in D as great as it would be to have such an implementation. We can't do everything, much as we might like to.

- Jonathan M Davis

May 05, 2014
On 04/05/14 23:20, Daniele M. wrote:

> You are right, devs would eventually abuse everything possible, although
> it would make it for sure more visible: you cannot advertize an un-@safe
> library as @safe, although I agree that a lot depends from devs/users
> culture.

In D, you can at least statically determine which part of the code is @safe and un-@safe.

-- 
/Jacob Carlborg
May 05, 2014
On Sun, 04 May 2014 13:29:33 +0000
Meta via Digitalmars-d <digitalmars-d@puremagic.com> wrote:
> The only language I would
> really trust is one in which it is impossible to write unsafe
> code, because you can then know that the developers can't use
> such unsafe hacks, even if they wanted to.

Realistically, I think that you ultimately have to rely on the developers doing a good job. Good tools help a great deal (including a programming language that's safe by default while still generally being efficient), but if you try and restrict the programmer such that they can only do things that are guaranteed to be safe, I think that you're bound to make it impossible to do a number of things, which tends to not only be very frustrating to the programmers, but it can also make it impossible to get the performance that you need in some circumstances.

So, while you might be able to better trust a library written in a language that's designed to make certain types of problems impossible, I don't think that it's realistic for that language to get used much in anything performance critical like an SSL implementation.

Ultimately, I think that the trick is to make things as safe as they can be without actually making it so that the programmer can't do what they need to be able to do. And while, I don't think that D hit the perfect balance on that one (e.g. we should have made @safe the default if we wanted that), I think that we've done a good job of it overall - certainly far better than C or C++.

- Jonathan M Davis
May 05, 2014
On Monday, 5 May 2014 at 06:35:07 UTC, Jonathan M Davis via Digitalmars-d wrote:
> On Sun, 04 May 2014 13:29:33 +0000
> Meta via Digitalmars-d <digitalmars-d@puremagic.com> wrote:
>> The only language I would
>> really trust is one in which it is impossible to write unsafe
>> code, because you can then know that the developers can't use
>> such unsafe hacks, even if they wanted to.
>
> Realistically, I think that you ultimately have to rely on the developers
> doing a good job. Good tools help a great deal (including a programming
> language that's safe by default while still generally being efficient), but if
> you try and restrict the programmer such that they can only do things that are
> guaranteed to be safe, I think that you're bound to make it impossible to do a
> number of things, which tends to not only be very frustrating to the
> programmers, but it can also make it impossible to get the performance that
> you need in some circumstances.
>
> So, while you might be able to better trust a library written in a language
> that's designed to make certain types of problems impossible, I don't think
> that it's realistic for that language to get used much in anything performance
> critical like an SSL implementation.
>
> Ultimately, I think that the trick is to make things as safe as they can be
> without actually making it so that the programmer can't do what they need to
> be able to do. And while, I don't think that D hit the perfect balance on that
> one (e.g. we should have made @safe the default if we wanted that), I think
> that we've done a good job of it overall - certainly far better than C or C++.
>
> - Jonathan M Davis

Sometimes I wonder how much money have C design decisions cost the industry in terms of anti-virus, static and dynamic analyzers tools, operating systems security enforcements, security research and so on.

All avoidable with bound checking by default and no implicit conversions between arrays and pointers.

--
Paulo
« First   ‹ Prev
1 2 3