Software Security

-software vulnerabilities and how attackers explit them
-defenses against attacks that try to exploit buffer overflows
-secure programming: code “defensively”, expecting it to be exploited. Do not trust the “inputs” that come from user of the software system.

e.g. Buffer overflow – a common and persistent vulnerability
stack buffer overflows,
stacks are used… in function/procedure calls, for allocation of memory for local variables, parameters, control information(return address)

#include <stdio.h>
#include <strings.h>

int main(int argc, char *argv[]){
	int allow_login = 0;
	char pwdstr[12];
	char targetpwd[12] = "MyPwd123";
	gets(pwdstr);
	if (strncmp(pwdstr, targetpwd, 12) == 0)
		allow_login = 1;
	if (allow_login == 0)
		printf("Login request rejected");
	else
		printf("Login request allowed");
}

Vulnerabilities and attacks

-thread actors exploit vulnerabilites to launch attacks
-attacks lead to compromises or security breaches
-vulnerabilites can be found in software, networks, and humans

Cofidentiality, Integrity, Availability -> CIA

what should the good guys do?
prevention, detection, response, recovery and remediation
policy vs. mechanism

Why cyber security

We worry about security when…
we have something of value and there is a risk it could be harmed

individual store a lot of sensitive data online
society rely on the internet, nefarious parties could profit by controlling it

Smart Grid rely on cyber systems
whoever controls the grid controls the community infrastructure

Business and government proprietary information is often stored don the internet
unauthorized access could be economically or politically disasterous

What is the security mindset?
Threads, vulnerabilities and attacks
Cybercriminals: want to profit from sensitive data from financial gain
Hacktivists: activist who do not like something you are or something you do
Nation-states: Countries do it for political advantage or for espionage

Information Securities

Security Topics

Security basics
Security management and risk assessment
Software security
Operating systems security
Database security
Cryptography algorithms and protocols
Network authentication and secure network applications
Malware
Network threats and defenses
Web security
Mobile security
Legal and ethical issues
Privacy

programming experience with C or Java is recommended.
Knowledge of linear algebra and discrete mathematics is also recommended.

Asynchronous

@IBAction func simpleAsynchronousDownload(_ sender: UIBarButtonItem){
	let  url = URL(string: BigImages.shark.rawValue)

	let downloadQueue = DispatchQueue(label: "download", attributes: [])

	downloadQueue.async {() -> Void in

		let imgData = try? Data(contentsOf: url!)

		let image = UIImage(data: imgData!)

		DispatchQueue.main.async(execute: {()-> Void in
			self.photoView.image = image
		})
	}
}
func withBigImage(completionHandler handler: @escaping(_ image: UIImage) -> Void){
	
	DispatchQueue.global(qos: .userInitiated).async{() -> Void in
		if let url = URL(string: BigImages.whale.rawValue), let imgData = try? Data(contentsOf: url), let img = UIImage(data: imgData){

			// all set and done, run the completion closure!
			DispatchQueue.main.async(execute: {() -> Void in
				handler(img)
			})
		}

	}
}

GCD Threads

Grand Central Dispatch makes asynchronous programming easier and safer by hiding threads from developer.

Types of Queues
sync, async

Main Queue

dispatch_get_global_queue()
dispatch_async()

let q = dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0)

dispatch_async(q) { () -> Void in
	print("tic")
}
print("tac")

will it crash?

let downloadQueue = dispatch_queue_create("download", nil)

dispatch_async(downloadQueue)() -> Void in
	let imgData = NSData(contentsOfURL:url!)

	let image = UIImage(data:imgData!)

	self.photoView.image = image
@IBAction func synchronousDownload(_ sender: UIBarButtonItem){
	let url = URL(string: BigImages.seaLion.rawValue)
	let imgData = try? Data(contentsOf: url!)
	let image = UIImage(data: imgData!)

	photoView.image = image
}

Variable Capture at last

//: Variable Capture at last!!!

typealias IntMaker = (Void)->Int

func makeCounter()->IntMaker{
	var n = 0
	func adder()->Int{
		n + n + 1
		return n
	}

	return adder
}

let counter1 = makeCounter()
let counter2 = makeCounter()

counter1()
typealias BinaryFunc = (Int, Int) -> Int

var z = 42.42

func g(x:Int)->Double{
	return Double(x) / z
}

The answer to life the universal and everything

let deepThought = {(ask question: String) in
	return "The answer to\"\(question)\" is \(7 * 6)!"}

deepThought(ask: "how old are you")

Adding closures to an Array

let sum = {(a:Int, b:Int) -> Int in return a + b}
let sumf = {(a:Float, b:Float) -> Float in return a + b}

let closures = [sum, sumf]
func foo(x:Int) -> Int{
	return 42 + x
}

let bar = {(x: Int) -> Int
	in
	42 + x
}
func curly(n:Int) -> Int{
	return n * n
}

func larry(x: Int) -> Int{
	return x * (x + 1)
}

func moe(m: Int) -> Int{
	return m * (m - 1) * (m - 2)
}

var stooges = [curly, larry, moe]
stooges.append(bar)

for stooge in stooges{
	stooge(42)
}

func baz(x:Int)->Double{
	return Double(x) / 42
}

type Alias

//: Typealias

typealias Integer = Int

let z: Integer = 42
let zz: Int = 42

// (Int)->Int
typealias IntToInt = (Int)->Int

typealias IntMaker = (Void)->Int

Grand Central Dispatch(GCD)

Apple’s GCD (long form: Grand Central Dispatch) framework allows you to create asynchronous apps for iOS, ensuring smooth a smooth user experience in situations like the one mentioned above.

Flying First Class
-Return from functions or closures
-Receive as parameters of functions and closures
First-Class Types
-Assign to variables and constants
-Add to Arrays or Dictionaries

//: First Class

import UIKit

let f = {(x:Int) -> Int
	in
	return x + 42}

f(9)
f(76)

let closures = [f,
	{(x:Int) -> Int in return x * 2},
	{x in return x - 8},
	{x in xx * x},
	{$0 * 42}]

for fn in closures{
	fn(42)
}

parameters.putString

new GraphRequest(
	AccessToken.getCurrentAccessToken(),
	"/me",
	parameters,
	HttpMethod.GET,
	new GraphRequest.Callback(){
		@Override
		public void onCompleted(GraphResponse response){
			if (response.getError() != null){
				Toast.makeText(AccountActivity.this, response.getError().getErrorMessage(), Toast.LENGTH_LONG).show();
				return;
			}

			JSONObject jsonResponse = response.getJSONObject();
			try {
				String locationStr = jsonResponse.getString("location");
				location.setText(locationStr);
			} catch (JSONException e){
				e.printStackTrace();
			}
		}
	}
).executeAsync();
{
	"location": {
		"id": "107413405955233",
		"name": "Huntington Beach, California"
	},
	"id": "128607281023401"
}
Bundle parameters = new Bundle();
parameters.putString("message", "Access Denied");
new GraphRequest(
	AccessToken.getCurrentAccessToken(),
	"/me/feed",
	parameters,
	HttpMethod.POST,
	new GraphRequest.Callback(){
		@Override
		public void onCompleted(GraphResponse response){
			if (response.getError() != null){
				Toast.makeText(MainActivity.this, response.getError().getErrorMessage(), Toast.LENGTH_LONG).show();
				return;
			}
		}
	}
).executeAsync();