Title: Class 10: SSL, Sharing, and Scheduling
Date: 2014-02-25
Category: Classes
Tags: operating systems, Apple fail, scheduling, round-robin, priority inversion
Author: David Evans

   <div class="todo">
**[Problem Set 3](|filename|../../pages/ps/ps3/ps3.md)** is due on
  Monday, 3 March.  You should have definitely at least finished Problem
  4 by today. 
[Note: extended to 5 March.]

</div>

<center>
<iframe src="http://www.slideshare.net/slideshow/embed_code/31725172" width="476" height="400" frameborder="2" marginwidth="0" marginheight="0" scrolling="no"></iframe><br>
[Videos](http://www.youtube.com/watch?v=cNJX34-yOW8&feature=share&list=PLvpsxlEF9cP2qc6W0tZZyy7eXsWIIpZdm)
</center>

# goto fail;

<center>
<iframe width="640" height="360" src="//www.youtube-nocookie.com/embed/cNJX34-yOW8?list=PLvpsxlEF9cP2qc6W0tZZyy7eXsWIIpZdm" frameborder="2" allowfullscreen></iframe>
</center>

Apple's SSL implementation (extracted from [sslKeyExchange.c](http://opensource.apple.com/source/Security/Security-55471/libsecurity_ssl/lib/sslKeyExchange.c?txt)): (original indentation and whitespace preserved for lack-of-clarity)

````C
static OSStatus
SSLVerifySignedServerKeyExchange(SSLContext *ctx, bool isRsa, SSLBuffer signedParams,
                                 uint8_t *signature, UInt16 signatureLen)
{
    OSStatus        err;
    SSLBuffer       hashOut, hashCtx, clientRandom, serverRandom;
    uint8_t         hashes[SSL_SHA1_DIGEST_LEN + SSL_MD5_DIGEST_LEN];
    SSLBuffer       signedHashes;
    uint8_t			*dataToSign;
	size_t			dataToSignLen;

	signedHashes.data = 0;
    hashCtx.data = 0;

    clientRandom.data = ctx->clientRandom;
    clientRandom.length = SSL_CLIENT_SRVR_RAND_SIZE;
    serverRandom.data = ctx->serverRandom;
    serverRandom.length = SSL_CLIENT_SRVR_RAND_SIZE;

    ...
	hashOut.data = hashes + SSL_MD5_DIGEST_LEN;
    hashOut.length = SSL_SHA1_DIGEST_LEN;
    if ((err = SSLFreeBuffer(&hashCtx)) != 0)
        goto fail;

    if ((err = ReadyHash(&SSLHashSHA1, &hashCtx)) != 0)
        goto fail;
    if ((err = SSLHashSHA1.update(&hashCtx, &clientRandom)) != 0)
        goto fail;
    if ((err = SSLHashSHA1.update(&hashCtx, &serverRandom)) != 0)
        goto fail;
    if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
        goto fail;
        goto fail;
    if ((err = SSLHashSHA1.final(&hashCtx, &hashOut)) != 0)
        goto fail;

	err = sslRawVerify(ctx,
                       ctx->peerPubKey,
                       dataToSign,				/* plaintext */
                       dataToSignLen,			/* plaintext length */
                       signature,
                       signatureLen);
	if(err) {
		sslErrorLog("SSLDecodeSignedServerKeyExchange: sslRawVerify "
                    "returned %d\n", (int)err);
		goto fail;
	}

fail:
    SSLFreeBuffer(&signedHashes);
    SSLFreeBuffer(&hashCtx);
    return err;

}
````

[Test your SSL implementation](https://www.imperialviolet.org:1266/) 
[Apple's Security Post](http://support.apple.com/kb/HT6147) 
[Apple's SSL/TLS Bug](https://www.imperialviolet.org/2014/02/22/applebug.html) (Adam Langley) 

What are differences between Rust and C that make this time of fail much less likely?
<div class="gap">

</div>

What software development practices should be followed for any production code to greatly reduce the chances of these types of mistakes not being caught?
<div class="gap">

</div>

## Theory Excursion

How hard is it to detect unreachable code in theory?
<div class="gap">

</div>

What should compilers do when desired decision procedures are
undecidable?
<div class="gap">

</div>

<center>
<table>
<tr>
<td>   
   <a href="http://www.dori-mic.org">
   <img src="http://www.dori-mic.org/images/dori-mic-cover-small.png" align="left" border=0></img>
   </a>
</td>
<td>
   Catch up on your theory with [Dori-Mic and the Universal Machine!](http://www.dori-mic.org)
</td>
</table>
</center>

# Sharing across Tasks

<center>
<iframe width="640" height="360" src="//www.youtube-nocookie.com/embed/MRbY4Ykd_yA?list=PLvpsxlEF9cP2qc6W0tZZyy7eXsWIIpZdm" frameborder="0" allowfullscreen></iframe>
</center>

[unsafe2.rs](|filename|./unsafe2.rs)

````rust
static mut count: uint = 0; 

fn update_count() { unsafe { count += 1; } }

fn main() {
    for _ in range(0u, 10) {
        spawn(proc() {
           for _ in range(0u, 1000) {
              update_count();
           }
        });
    }
    println!("Count: {:}", unsafe { count });
}
````

Assembly code produced by `rustc -S unsafe2.rs`: [unsafe2.s](|filename|./unsafe2.s)
````
	.section	__TEXT,__text,regular,pure_instructions
	.align	4, 0x90
__ZN12update_count19h86817af0b0797e96al4v0.0E:
	.cfi_startproc
	cmpq	%gs:816, %rsp
	ja	LBB0_0
	movabsq	$16, %r10
	movabsq	$0, %r11
	callq	___morestack
	ret
LBB0_0:
	pushq	%rbp
Ltmp2:
	.cfi_def_cfa_offset 16
Ltmp3:
	.cfi_offset %rbp, -16
	movq	%rsp, %rbp
Ltmp4:
	.cfi_def_cfa_register %rbp
	pushq	%rax
	movq	__ZN5count19hc6afed277fb1b6c3ah4v0.0E(%rip), %rax
	addq	$1, %rax
	movq	%rax, __ZN5count19hc6afed277fb1b6c3ah4v0.0E(%rip)
	movq	%rdi, -8(%rbp)
	addq	$8, %rsp
	popq	%rbp
	ret
	.cfi_endproc
...
````

Where is the data race?
<div class="gap">

</div>

[RWArc](http://static.rust-lang.org/doc/0.9/extra/arc/struct.RWArc.html) - automatically reference-counter storage protected by a reader-writer lock

Why is it useful to have separate locks for reading and writing?
<div class="gap">


</div>

````rust
extern mod extra; 
use extra::arc::RWArc;

fn update_count(counter: RWArc<int>) {
    counter.write(|count: &mut int| { *count += 1; });
}

fn main() {
    let counter: RWArc<int> = RWArc::new(0);

    for _ in range(0, 10) {
        let ccounter = counter.clone(); // need the clone - this moves counter
        spawn(proc() {
           for _ in range(0, 1000) {
              update_count(ccounter.clone()); // shouldn't be necessary!?
           }
        });
    }

    counter.read(|count| {
       println!("Count: {:d}", *count);
    });

}
````

Why is the printed count still not 10000?
<div class="gap">

</div>

# Scheduling

<center>
<iframe width="640" height="360" src="//www.youtube-nocookie.com/embed/F4LUFdWg2bQ?list=PLvpsxlEF9cP2qc6W0tZZyy7eXsWIIpZdm" frameborder="2" allowfullscreen></iframe>
</center>

What are the two main decisions the supervisor needs to make on each
kernel timer interrupt?

<div class="gap">
</div>


Max Ehrmann's ["Desiderata"](http://en.wikipedia.org/wiki/Desiderata):

> Go placidly amid the noise and haste, and remember what peace there
  may be in silence. As far as possible without surrender be on good
  terms with all persons. Speak your truth quietly and clearly; and
  listen to others, even the dull and the ignorant; they too have their
  story. Avoid loud and aggressive persons, they are vexations to the
  spirit... Exercise caution in your business affairs; for the world is
  full of trickery... And whether or not it is clear to you, no doubt the
  universe is unfolding as it should... whatever your labors and
  aspirations, in the noisy confusion of life keep peace with your
  soul. With all its sham, drudgery, and broken dreams, it is still a
  beautiful world. Be cheerful. Strive to be happy.


Why is there a fundamental tradeoff between _maximizing resource
utilization_ and _fairness_ in scheduling?

<div class="gap">
</div>



<div id="disqus_thread"></div>

<script type="text/javascript">
        /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
        var disqus_shortname = 'rust-class'; // required: replace example with your forum shortname
	var disqus_url = 'http://www.rust-class.org/class-10-ssl-sharing-and-scheduling.html';

        /* * * DON'T EDIT BELOW THIS LINE * * */
        (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
        })();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>



