70
Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Ap A Structural Operational Semantics for JavaScript Ankur Taly Dept. of Computer Science, Stanford University Ankur Taly A Structural Operational Semantics for JavaScript

A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

  • Upload
    doxuyen

  • View
    215

  • Download
    2

Embed Size (px)

Citation preview

Page 1: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

A Structural Operational Semantics for JavaScript

Ankur Taly

Dept. of Computer Science, Stanford University

Ankur Taly A Structural Operational Semantics for JavaScript

Page 2: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

1 Need for a Formal Semantics ?

2 Structural Operational Semantics for IMPFormalizing the program stateSemantic RulesFormal properties

3 Structural Operational Semantics for JavaScriptMotivationMain features of JavaScriptFormalizing the Program stateSemantic RulesFormal Properties

4 Applications of the Operational Semantics for JavaScriptBeamAuthADSafe

5 Conclusions and Future workAnkur Taly A Structural Operational Semantics for JavaScript

Page 3: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Syntax and Semantics

Every programming language has two broad components

Syntax

All possible strings in the language.Structure of the strings usually given by a Formal grammar.Example: if (x > 10) { x = x +1;}

Semantics

Meaning of the strings in the language.Actions that occur while interpreting these strings.Meaning [if (x > 10) { x = x +1;}] = If value of x is greaterthan 10 then x is incremented by 1.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 4: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Interpreter

Convert Syntax to Machine Code (in accordance with thesemantics).

SyntaxSemantics−→ Machine Code

Generate machine code so that the correct sequence ofactions occur

Example : x = x + 10

Find where the variable x is stored in memory.Add 10 to the value.Place the new value back in memory.

We are not interested in this conversion process but only inspecifying the semantics using some mathematical object.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 5: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Informal and Formal Semantics

Informal semantics :

Meaning in English.

Language standard or specification manual (Javascript :ECMA 2.62)

Sufficient for ’understanding’ the language but insufficient forrigorously proving properties about the language

Prove or Disprove : For all terms t, the execution of t onlydepends on the values of the variables appearing in t.

Does meaning[x = x + 10] only depend on value of x ? in C ?in JavaScript ?

Ankur Taly A Structural Operational Semantics for JavaScript

Page 6: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

A corner case

var y = "a";var x = {toString : function(){ return y;}}

x = x + 10;js> "a10"

Implicit type conversion of an object to a string in JavaScriptinvolves calling the toString function.

Informal semantics fail to bring out such corner cases.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 7: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Informal and Formal Semantics

Formal Semantics :

Specify meaning in a Mathematically rigorous way.

Provides a framework for proving properties of the kindmentioned on the previous slide.

Task : Convert informal semantics into a formal semantics.

The very act of formalization reveals subtle aspects of thelanguage.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 8: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Real World Example

Isolating two pieces of JavaScript code :

Web pages today serve content coming from multiplelocations :Example : Advertisement code obtained from various adnetworks.

Some of this content may be untrusted : Ad code mightcontain a malicious script

var v = document.getElementsByName(”password”)[0].value

Allow untrusted code to perform valuable interactions while atthe same preventing intrusion and malicious damage.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 9: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Real World Example

Figure: Trusted and Untrusted code

Ankur Taly A Structural Operational Semantics for JavaScript

Page 10: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Real World Example

Figure: Web Mashup (Valuable Interaction)

Ankur Taly A Structural Operational Semantics for JavaScript

Page 11: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Formulating the problem

Problem 1

Write a static analyzer that can check an untrusted JavaScriptprogram and determine if it is malicious.

Very difficult to statically determine the meaning of a JavaScriptprogram because of the presence of functions that can convertstring to code and vice versa, for eg : eval.

Problem 2

Find a well-defined and meaningful subset of JavaScript for whichthe above problem is solvable

Seems approachable : Identify the bad guys like ’eval’ and get ridof them.Formal Semantics of JavaScript is the first step towards solving theabove problems.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 12: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Formulating the problem

Problem 1

Write a static analyzer that can check an untrusted JavaScriptprogram and determine if it is malicious.

Very difficult to statically determine the meaning of a JavaScriptprogram because of the presence of functions that can convertstring to code and vice versa, for eg : eval.

Problem 2

Find a well-defined and meaningful subset of JavaScript for whichthe above problem is solvable

Seems approachable : Identify the bad guys like ’eval’ and get ridof them.Formal Semantics of JavaScript is the first step towards solving theabove problems.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 13: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Applications of Formal Semantics

Reveal inconsistencies in the language standard/spec.

Prove properties about specific programs, static analysis ofprograms.

Prove properties involving quantification over all possibleprograms.

Proving program equivalence.

Design sound compiler optimizations.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 14: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Operational Semantics

Meaning of a program ⇔ sequence of actions that are takenduring its execution.

Specify sequence of actions as transitions of an Abstract Statemachine

States corresponds to

Term being evaluatedAbstract description of memory and other data structuresinvolved in computation.

A state transition denotes a partial evaluation of the term.

Based on the abstract syntax of the language.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 15: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Another approach

Denotational Semantics :

Specify meaning of a program by specifying the partialfunction represented by each syntactic construct.

The function could be from one memory state to another.

Example : The denotation of the term

y = 1; x = 10; while(x >= 1){y = x ∗ y; x = x− 1};

is a partial function from one abstract memory state to another.The final state would be the initial state except that the value of xwould be 0 and the value of y would be factorial(10).

Ankur Taly A Structural Operational Semantics for JavaScript

Page 16: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

This lecture

Structural Operational Semantics (SOS).

Systematic way of specifying operational semantics.

Specify the transitions in a syntax oriented manner using theinductive nature of the abstract syntax.

Example : The state transition for the term e1 + e2 should bedescribed using the transition for e1 and the transition for e2.

We will first look at the SOS of a simple imperative language(IMP) and then extend it to JavaScript.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 17: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

The IMP Language

Syntax

Aexp : a ::= n | X | a + a | a * a

Bexp : b ::= t | a <= a | not b | b and b

Stmt : s ::= skip | X := a | s; s |

if b then s else s | while b do s

X is the set of variables (fixed) and n varies over I.

Informal Semantics is very intuitive

Ankur Taly A Structural Operational Semantics for JavaScript

Page 18: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Formal Semantics of IMP : Notion of a State

Represented as a pair S = 〈t, σ〉t : term being evaluated (expressed using abstract syntax)σ : abstract description of memory.

For IMP, we can abstract the memory as a store, which is amapping from variable names to values ie σ : X → ITerminal states are those for which the term t is empty or is avalue.

We will add more information to the state when we move toJavaScript.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 19: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Transition Rule : Example

A rule for Arithmetic Expressions

〈a1, σ〉 → 〈a′1, σ〉〈a1 + a2, σ〉 → 〈a′1 + a2, σ〉

[A3a]〈a2, σ〉 → 〈a′2, σ〉

〈n + a2, σ〉 → 〈n + a′2, σ〉[A3b]

How to interpret this rule ?

If the term a1 partially evaluates to a′1 then a1 + a2 partiallyevaluates to a′1 + a2.

Once the expression a1 reduces to a value n, then startevaluating a2

Example :

〈(10 + 12) + (13 + 20), σ〉 A3a−→ 〈22 + (13 + 20), σ〉 A3b−→ 〈22 + 33, σ〉

Ankur Taly A Structural Operational Semantics for JavaScript

Page 20: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Transition Rule : Example

A rule for Statements

〈a, σ〉 → 〈a′, σ′〉〈x := a, σ〉 → 〈x = a′, σ′〉

[C3]σ′ = Put(σ, x , n)

〈x := n, σ〉 → 〈σ′〉[C2]

How to interpret this rule ?

If the arithmetic exp a partially evaluates to a′ then thestatement x = a partially evaluates to x = a′.

Rule C2 applies when a reduces to a value n.

Put(σ, x , n) updates the value of x to n.

Example : 〈(x := 10 + 12, σ〉 C3−→ 〈x := 22, σ〉 C2−→ 〈σ′〉

Ankur Taly A Structural Operational Semantics for JavaScript

Page 21: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Formally

General form of transition rule:

P1, . . . ,Pn

〈t, σ〉 → 〈t ′, σ′〉P1, . . . ,Pn

〈t, σ〉 → σ′(1)

P1, . . . ,Pn are the conditions that must hold for the transition togo through. Also called the premise for the rule. These could be

Other transitions corresponding to the sub-terms.

Predicates that must be true.

Calls to meta functions like :

get(σ, x) = v : Fetch the value of x .put(σ, x , n) = σ′ : Update value of x to n and return newstore.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 22: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Arithmatic Expressions

Aexp : a ::= n | X | a + a | a * a

Rules

〈n, σ〉 : Terminal[A1]v = get(σ, x)

〈x , σ〉 → 〈v , σ〉[A2]

〈a1, σ〉 → 〈a′1, σ〉〈a1 ∗ a2, σ〉 → 〈a′1 ∗ a2, σ〉

[A4a]〈a2, σ〉 → 〈a′2, σ〉

〈n ∗ a2, σ〉 → 〈n ∗ a′2, σ〉[A4b]

Example

Consider the store σ = {x : 10, y : 20}. The state 〈x + y , σ〉 hasthe following trace.

〈x ∗ y , σ〉 A2→ 〈10 ∗ y , σ〉 A2→ 〈10 ∗ 20, σ〉

Ankur Taly A Structural Operational Semantics for JavaScript

Page 23: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Commands

Com : c ::= skip | X := a | c; c |

if b then c else c | while b do c

rules

〈a, σ〉 → 〈a′, σ′〉〈x := a, σ〉 → 〈x = a′, σ′〉

[C3]σ′ = Put(σ, x , n)

〈x := n, σ〉 → 〈σ′〉[C2]

〈s1, σ〉 → 〈s′1, σ′〉〈s1; s2, σ〉 → 〈s′1; s2, σ′〉

[C4a]〈s1, σ〉 → 〈σ′〉

〈s1; s2, σ〉 → 〈s2, σ′〉[C4b]

Ankur Taly A Structural Operational Semantics for JavaScript

Page 24: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Commands

If Then Else

〈if tt then s1 else s2, σ〉 → 〈s1, σ〉[C5a]〈if ff then s1 else s2, σ〉 → 〈s2, σ〉[C5b]

〈b, σ〉 → 〈b′, σ〉〈if b then s1 else s2, σ〉 → 〈ifb′ then s1 else s2, σ〉

[C5c]

While

〈while b do s, σ〉 →〈if b then s; while b s else skip end, σ〉[C6]

Ankur Taly A Structural Operational Semantics for JavaScript

Page 25: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Context Sensitive Rules (Felleisen)

Similar rules

〈a1, σ〉 → 〈a′1, σ〉〈a1 + a2, σ〉 → 〈a′1 + a2, σ〉

[A3a]〈a2, σ〉 → 〈a′2, σ〉

〈n + a2, σ〉 → 〈n + a′2, σ〉[A3b]

〈a1, σ〉 → 〈a′1, σ〉〈a1 ∗ a2, σ〉 → 〈a′1 ∗ a2, σ〉

[A4a]〈a2, σ〉 → 〈a′2, σ〉

〈n ∗ a2, σ〉 → 〈n ∗ a′2, σ〉[A4b]

The above rules have a similar premise :

Combine them into a single rule of the following form :

〈a, σ〉 → 〈a′, σ〉AC (a)→ AC (a′)

where AC :: | + a|n + | ∗ a|n ∗

Ankur Taly A Structural Operational Semantics for JavaScript

Page 26: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Context Rules

Reduces the number of rules.

Replace all rules which have a transition in their premise by anappropriate context rule. Very helpful while proving propertiesby induction.

Contexts can also be used to specify continuations (more onthis when we move to JavaScript).

Ankur Taly A Structural Operational Semantics for JavaScript

Page 27: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Proving Properties : Structural Induction

Progress

For all a ∈ Aexp, states σ:

a is not a Value ⇒ ∃a′, σ′ : 〈a, σ〉 → 〈a′, σ′〉

Proof Technique : Induction over the structure of a.

Base Case : : a := n and a := x

Inductive Cases : a := a1 + a2, a := a1 ∗ a2.

Assume Induction hypothesis for a1 and a2

Show that a reduction rule applies to a if there exists areduction for a1 and a2.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 28: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Proving Properties : Rule based Induction

Let Var(t) denotes set of variable names appearing in term t.For Ex : Var(if(x >= 0){y = y + 1}) = {x , y}.

Reachability

For all t, t ′ ∈ Terms, states σ, σ′ :

∀x /∈ Var(t) : 〈t, σ〉 → 〈t ′, σ′〉 ⇒ σ(x) = σ′(x)

Proof Technique : This is a property of a reduction step. Inductover the set of transition rules.

Base Case : Rules with no transition in their premise.

Inductive Case : Context rules.

Later, we will state a similar property for JavaScript.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 29: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

1 Need for a Formal Semantics ?

2 Structural Operational Semantics for IMPFormalizing the program stateSemantic RulesFormal properties

3 Structural Operational Semantics for JavaScriptMotivationMain features of JavaScriptFormalizing the Program stateSemantic RulesFormal Properties

4 Applications of the Operational Semantics for JavaScriptBeamAuthADSafe

5 Conclusions and Future workAnkur Taly A Structural Operational Semantics for JavaScript

Page 30: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Semantics of JavaScript: Motivation

Widely used web programming language.

Several subtle features that most programmers are unaware of.

Very important to fully understand the semantics so as toreason about the security properties of programs written in it.

Example 1

var b = 10;var f = function(){ var b = 5;

function g(){var b = 8; return this.b;};g();}

var result = f();

Result = 10

Ankur Taly A Structural Operational Semantics for JavaScript

Page 31: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Semantics of JavaScript: Motivation

Widely used web programming language.

Several subtle features that most programmers are unaware of.

Very important to fully understand the semantics so as toreason about the security properties of programs written in it.

Example 1

var b = 10;var f = function(){ var b = 5;

function g(){var b = 8; return this.b;};g();}

var result = f();

Result = 10

Ankur Taly A Structural Operational Semantics for JavaScript

Page 32: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Another Example

Example 2

var f = function(){ var a = g();function g() { return 1;};function g() { return 2;};var g = function() { return 3;}return a;}

var result = f();

What is the final value of result ?

result = 2

Ankur Taly A Structural Operational Semantics for JavaScript

Page 33: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Another Example

Example 2

var f = function(){ var a = g();function g() { return 1;};function g() { return 2;};var g = function() { return 3;}return a;}

var result = f();

What is the final value of result ?result = 2

Ankur Taly A Structural Operational Semantics for JavaScript

Page 34: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Informal Semantics

ECMA 2.62 Spec.

Important Concepts

Prototype based lookup for object propertiesFunction Closures.Function calls involve parsing the body of the function beforecalling it.for (p in o){....} , eval(...), o[s] allow string tobe used as code and vice versa.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 35: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Basic JavaScript Syntax

Syntax

According to ECMA 2.62 :

Expressions (e) :: this | x | e OP e | e(e ) |new e(e ) |...

Statement (s) :: "s*" | if (e) s else s |while (e) s | with (e) s | ...

Programs (P) :: s P | fd PFunction Decl (fd) :: function x (x ){ P }

Observation

Observe that according to the spec, declaring a function inside an’if’ block is a syntax error !

Ankur Taly A Structural Operational Semantics for JavaScript

Page 36: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Basic JavaScript Syntax

Syntax

According to ECMA 2.62 :

Expressions (e) :: this | x | e OP e | e(e ) |new e(e ) |...

Statement (s) :: "s*" | if (e) s else s |while (e) s | with (e) s | ...

Programs (P) :: s P | fd PFunction Decl (fd) :: function x (x ){ P }

Observation

Observe that according to the spec, declaring a function inside an’if’ block is a syntax error !

Ankur Taly A Structural Operational Semantics for JavaScript

Page 37: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

JavaScript : Key Features

Everything (including functions) is either an object or aprimitive value.

Activation records are normal JavaScript objects and thevariable declarations are properties of this object.

All computation happens inside a global object which is alsothe initial activation object.

Instead of a stack of activation records, there a chain ofactivation records, which is called the scope chain.

Arbitrary objects can be placed over the scope chain.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 38: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Formal Semantics : Program state

All objects are passed by reference ⇒ The store must haveinformation about Heap locations.

Variables have different values in different scopes ⇒ Statemust include info about current scope.

State

Program state is represented as a triple 〈H, l , t〉.H : Denotes the Heap, mapping from the set of locations(L)to objects.

l : Location of the current scope object (or current activationrecord).

t : Term being evaluated.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 39: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Heap and Heap Reachability Graph

Figure: Heap and its reachability graph

Heap Reachability Graph : Heap addresses are the nodes. An edgefrom li to lj , if the object at address li has property p pointing to lj .

Ankur Taly A Structural Operational Semantics for JavaScript

Page 40: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Heap Operations

Each Heap object o is a record {p1 : ov1, . . . , pn : ovn} where pi

are property names and ovi could be a primitive value or anotherheap addresses.

Dot(H, l , p) = l1 : Access the property p of object at heaplocation l .

Put(H, l , p, lv ) = H ′ : Update the property p of object atH(l) and return the new Heap.

H ′, l = alloc(H, o) : Allocate object o to a new heap locationl .

Ankur Taly A Structural Operational Semantics for JavaScript

Page 41: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Identifier Resolution

How do we find the value of an identifier ’x’ appearing in theprogram ?In the case of IMP we have the rule :

v = get(σ, x)

〈x , σ〉 → 〈v , σ〉

For JavaScript it involves traversing the scope chain.

Recall : following of access links to resolve local variables.

In the case of JavaScript scope chain can also have arbitraryobjects in addition to activation records.

”x” gets resolved to a pair l ∗ ”x” where l”hasproperty”x.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 42: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Some Notation

o "hasProperty" p ⇔ ”p” is a property of object ”o” orone of the ancestoral prototypes of ”o”.

o "hasOwnProperty" p ⇔ ”p” is a property of object ”o”itself.

A JavaScript reference type is pair denoted as l ∗ p where l isheap address, also called the base type of the reference, and pis a property name.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 43: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Scope and Prototype lookup

Figure: Scope and Prototype lookup

Every scope chain has the global object at its base.Every prototype chain has Object.prototype at the top, whichis a native object containing predefined functions such astoString, hasOwnProperty etc.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 44: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Scope lookup : Rules

ECMA 2.62 :

1 Get the next object (l) inthe scope chain. If thereisn’t one, goto 4.

2 If l ”HasProperty” x, returna reference type l*”x”.

3 Else, goto 1

4 Return null*x.

Scope(H, l, ”x”) = ln

〈H, l, x〉 → 〈H, l, ln ∗ ”x”〉

HasProperty(H, l, m)

Scope(H, l, m) = l

¬(HasProperty(H, l, m))H(l).@Scope = ln

Scope(H, l, m) = Scope(H, ln, m)

Scope(H, null, m) = null

Ankur Taly A Structural Operational Semantics for JavaScript

Page 45: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Prototype lookup : Rules

ECMA 2.62 :

1 If base type is null, throwa ReferenceErrorexception.

2 Else, Call the Getmethod , passing propname(x) and base type las arguments.

3 Return result(2).

H2, lexcp = alloc(H, o)o = newNativeErr(””,#RefErrProt)

〈H, l, (null ∗ m)〉 → 〈H2, l, 〈lexcp〉〉

Get(H, l, m) = va

〈H, l, ln ∗ m〉 → 〈H, l, va〉

HasOwnProperty(H, l, m)Dot(H, l, m) = va

Get(H, l, m) = va

¬(HasOwnProperty(H, l, m))H(l).@prototype = lp

Get(H, l, m) = Get(H, lp, m)

Ankur Taly A Structural Operational Semantics for JavaScript

Page 46: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Exceptions

What if an intermediate step gives an exception ?

We need to stop further evaluation and throw the exceptionto the top level.

Example :〈H, l, a0〉 → 〈H, l, 〈lexcp〉〉

〈H, l, a0 + a1〉 → 〈H, l, 〈lexcp〉+ a1〉Stop evaluation of a2.

Context Rule for Exception

〈H, l, eC[〈lexcp〉]〉 → 〈H, l, 〈lexcp〉〉

where eC ::= | eC OP e | va OP eC | eC[e] | ...

We must force all other rules to NOT apply to terms withexception 〈lexcp〉 so that determinism is preserved.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 47: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Exceptions

What if an intermediate step gives an exception ?

We need to stop further evaluation and throw the exceptionto the top level.

Example :〈H, l, a0〉 → 〈H, l, 〈lexcp〉〉

〈H, l, a0 + a1〉 → 〈H, l, 〈lexcp〉+ a1〉Stop evaluation of a2.

Context Rule for Exception

〈H, l, eC[〈lexcp〉]〉 → 〈H, l, 〈lexcp〉〉

where eC ::= | eC OP e | va OP eC | eC[e] | ...

We must force all other rules to NOT apply to terms withexception 〈lexcp〉 so that determinism is preserved.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 48: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

With statement

With statement allows arbitrary objects to be placed on top of thescope chain.Example :

var a = 5;var o = {a:10}with(o){ a; }// 10

A simple rule for with is :

〈H, l, with(lnew)s〉 → 〈H, lnew, s〉

Is the above rule correct ?Observe that once with completes, we need to restore the oldscope back !

Ankur Taly A Structural Operational Semantics for JavaScript

Page 49: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Continuation as contexts

Lets create a new context $with(l, ) . The new rule is

〈H, l, with(lnew)s〉 → 〈H, lnew, $with(l, s)〉

Then we have separate context rules.

〈H, l, s〉 → 〈H′, l′, s′〉〈H, l, $with(lold, s)〉 → 〈H′, l′, $with(lold, s′)〉

[With − s]

〈H, l, s〉 → 〈H′, l′, val〉〈H, l, $with(lold, s)〉 → 〈H′, lold, val〉

[With − end ]

$with(l, ) represents some intermediate state reached duringhe reduction of the with statement.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 50: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Formal Properties and Theorems

Notations and Definitions :

Wf(〈H, l, t〉) : Predicate denoting well-formedness of state(〈H, l, t〉)G(H) : Heap reachability graph of H.

Theorem 1

Progress : Wf(S)∧ S is not a terminal state⇒ (∃S ′ : S → S ′)Proof : Structural Induction.

Preservation : Wf(S) ∧ S → S ′ ⇒ Wf(S ′).Proof : Induction over the rules.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 51: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Garbage Collector for JavaScript

Set of live heap addresses : ∆(〈H0, l0, t0〉) = {l |l ∈ t0} ∪ {l0}(Recall Var(t) in the case of IMP)

Theorem 2

Evaluation of a state S = 〈H, l , t〉 only depends on the portion ofthe heap reachable from the Heap addresses ∆(S).

Immediate Consequence : Mark and sweep garbage collector.

Garbage collect all heap addresses not reachable from ∆(S) inthe heap reachability graph.

By theorem 2, the semantics of S is preserved during garbagecollection.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 52: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Open Problem : α-renaming

α renaming a program refers to renaming all the identifiersappearing in the program.

Done by FBJS as a security measure. Prepends all identifierswith user’s application id.

Will this change preserve the meaning of the program ? in C ?in JavaScript ?

No, think about eval !

Problem

Find a meaningful subset of JavaScript such that the semantics ofany program written in that subset would be invariant under sucha renaming.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 53: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Open Problem : α-renaming

α renaming a program refers to renaming all the identifiersappearing in the program.

Done by FBJS as a security measure. Prepends all identifierswith user’s application id.

Will this change preserve the meaning of the program ? in C ?in JavaScript ?

No, think about eval !

Problem

Find a meaningful subset of JavaScript such that the semantics ofany program written in that subset would be invariant under sucha renaming.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 54: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

1 Need for a Formal Semantics ?

2 Structural Operational Semantics for IMPFormalizing the program stateSemantic RulesFormal properties

3 Structural Operational Semantics for JavaScriptMotivationMain features of JavaScriptFormalizing the Program stateSemantic RulesFormal Properties

4 Applications of the Operational Semantics for JavaScriptBeamAuthADSafe

5 Conclusions and Future workAnkur Taly A Structural Operational Semantics for JavaScript

Page 55: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Real Application : BeamAuth (Ben Adida)

Two factor authentication mechanism, proposed by BenAdida.

Consider visiting the login page of your bank (might be aphishing page!).

A piece of JavaScript sits in one of your bookmarks whichcontains yours login information.

When the bookmarklet is clicked, it verifies whether thesource of the page is www.bank.com and then fills in the logininfo on your behalf.

Problem

Write a JavaScript program which can check whether a pagebelongs to the hostname bank.com.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 56: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Solution

Attempt 1 - Seems Easy

If (window.location.host === "bank.com") return "goodpage" else return "bad page"

Attack : var window = {location:{host: "bank.com"}}Mitigation : Use this instead of window.

Attempt 2 - I can fix it

If (this.location.host === "bank.com") return "goodpage" else return "bad page"

Attack : window. defineGetter ("location", function(){return {host : "bank.com"}})Mitigation: Use lookupGetter

Ankur Taly A Structural Operational Semantics for JavaScript

Page 57: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Solution

Attempt 1 - Seems Easy

If (window.location.host === "bank.com") return "goodpage" else return "bad page"

Attack : var window = {location:{host: "bank.com"}}Mitigation : Use this instead of window.

Attempt 2 - I can fix it

If (this.location.host === "bank.com") return "goodpage" else return "bad page"

Attack : window. defineGetter ("location", function(){return {host : "bank.com"}})Mitigation: Use lookupGetter

Ankur Taly A Structural Operational Semantics for JavaScript

Page 58: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Solution

Attempt 1 - Seems Easy

If (window.location.host === "bank.com") return "goodpage" else return "bad page"

Attack : var window = {location:{host: "bank.com"}}Mitigation : Use this instead of window.

Attempt 2 - I can fix it

If (this.location.host === "bank.com") return "goodpage" else return "bad page"

Attack : window. defineGetter ("location", function(){return {host : "bank.com"}})Mitigation: Use lookupGetter

Ankur Taly A Structural Operational Semantics for JavaScript

Page 59: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Solution

Attempt 1 - Seems Easy

If (window.location.host === "bank.com") return "goodpage" else return "bad page"

Attack : var window = {location:{host: "bank.com"}}Mitigation : Use this instead of window.

Attempt 2 - I can fix it

If (this.location.host === "bank.com") return "goodpage" else return "bad page"

Attack : window. defineGetter ("location", function(){return {host : "bank.com"}})Mitigation: Use lookupGetter

Ankur Taly A Structural Operational Semantics for JavaScript

Page 60: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Solution

Attempt 1 - Seems Easy

If (window.location.host === "bank.com") return "goodpage" else return "bad page"

Attack : var window = {location:{host: "bank.com"}}Mitigation : Use this instead of window.

Attempt 2 - I can fix it

If (this.location.host === "bank.com") return "goodpage" else return "bad page"

Attack : window. defineGetter ("location", function(){return {host : "bank.com"}})Mitigation: Use lookupGetter

Ankur Taly A Structural Operational Semantics for JavaScript

Page 61: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Cat and mouse game went on

Attempt 15 - Does this work ?

function isNormalProperty(obj, propertyName){

for (var p in obj){

if (p === "__lookupGetter__" ) return false;

if (p === "__lookupSetter__") return false; }

if (typeof obj.__lookupGetter__("__lookupGetter__") !== undefined)

return false;

if (this.__lookupGetter__("__lookupSetter__") !== undefined)

return false; }

if (!isNormalProperty(this, "location")) return "Attack! ;

if (!isNormalProperty("#", "host")) return Attack;

this.location = "#";

if (this.__lookupGetter__("location") === "beamauth.org")

doLoginStuff(SECRET_TOKEN);

Attack : lookupGetter could be redefined :-(

Ankur Taly A Structural Operational Semantics for JavaScript

Page 62: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Cat and mouse game went on

Attempt 15 - Does this work ?

function isNormalProperty(obj, propertyName){

for (var p in obj){

if (p === "__lookupGetter__" ) return false;

if (p === "__lookupSetter__") return false; }

if (typeof obj.__lookupGetter__("__lookupGetter__") !== undefined)

return false;

if (this.__lookupGetter__("__lookupSetter__") !== undefined)

return false; }

if (!isNormalProperty(this, "location")) return "Attack! ;

if (!isNormalProperty("#", "host")) return Attack;

this.location = "#";

if (this.__lookupGetter__("location") === "beamauth.org")

doLoginStuff(SECRET_TOKEN);

Attack : lookupGetter could be redefined :-(

Ankur Taly A Structural Operational Semantics for JavaScript

Page 63: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

May be its impossible !!!

Hgood : Heap state when actual bank.com is loaded.

Hbad : Heap state when the phishing page is loaded.

Two notions of impossibility

Weak Notion : For every BeamAuth script that we can write,there exists a bad page that can still go through.

Strong Notion : There exists a phishing page for which theheap state Hbad−strong is such that :

Hbad−strong is observationally equivalent to Hgood.No BeamAuth can distinguish Hbad−strong from Hgood

The Strong Notion is more powerful than the weak one.The Operational semantics of JavaScript is the first step towardsformally proving such results.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 64: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

ADSafe (Douglas Crockford)

ADSafe is a solution proposed by Yahoo for controlling theinteraction between the trusted and untrusted code.

Basic Idea :1 Represents a safe subset of JavaScript.2 Wraps untrusted code inside a safe object called ADSafe

object.3 All interaction with the trusted code happens only using the

methods in the ADSafe object.4 Untrusted code can be statically checked to ensure that it only

calls methods of the ADSafe object (Tool : JSLint).

More information on http:www.adsafe.org

Ankur Taly A Structural Operational Semantics for JavaScript

Page 65: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

ADSafe (First version)

var ADSAFE = function () {

....

var reject = function (object, name) {

return object === window || typeof object !== ’object’ ||

(typeof name !== ’number’ &&

(typeof name !== ’string’ || name.charAt(0) == ’_’));};

return {

get: function (object, name) {

var value;

if (!reject(object, name) && object.hasOwnProperty(name)) {

value = object[name];

if (typeof value !== ’function’ && value !== window) {

return value; }}

error(); },

set : function(object,name,value){ .... }

};}();

Usage

To access property p of object o : ADSAFE.get(o, p)

Ankur Taly A Structural Operational Semantics for JavaScript

Page 66: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Challenges and Issues

Consider the following property : ”All interaction with thetrusted code happens only using the methods in the ADSafeobject.”Is this achievable ?

Consider the following code :

var o = {a:10};var arr = [10,11];arr[o];

This function implicitly calls Object.prototype.toString,which is a function defined in the trusted space.

What if toString in turn leaks out pointer to global object ?

Conclusion

Besides the untrusted code, ADSafe has to impose restrictions onthe native functions and objects present in the trusted space.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 67: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Challenges and Issues

Consider the following property : ”All interaction with thetrusted code happens only using the methods in the ADSafeobject.”Is this achievable ?

Consider the following code :

var o = {a:10};var arr = [10,11];arr[o];

This function implicitly calls Object.prototype.toString,which is a function defined in the trusted space.

What if toString in turn leaks out pointer to global object ?

Conclusion

Besides the untrusted code, ADSafe has to impose restrictions onthe native functions and objects present in the trusted space.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 68: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Conclusions and Future work

Conclusions :

First step towards formal analysis of whole of JavaScript.

We have formalized the entire ECMA 2.62 language.Complete set of rules (in ASCII) span 70 pages.

Prove basic soundness properties like progress andpreservation for the semantics and the fact that JavaScriptisgarbage collectible.

Future Work :

Add features like setters/getters (not present in ECMA 2.62),which are present in various browsers.

Formalize interaction with the DOM.

Apply the semantics for security analysis of applications suchas ADSafe and BeamAuth.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 69: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Conclusions and Future work

Conclusions :

First step towards formal analysis of whole of JavaScript.

We have formalized the entire ECMA 2.62 language.Complete set of rules (in ASCII) span 70 pages.

Prove basic soundness properties like progress andpreservation for the semantics and the fact that JavaScriptisgarbage collectible.

Future Work :

Add features like setters/getters (not present in ECMA 2.62),which are present in various browsers.

Formalize interaction with the DOM.

Apply the semantics for security analysis of applications suchas ADSafe and BeamAuth.

Ankur Taly A Structural Operational Semantics for JavaScript

Page 70: A Structural Operational Semantics for JavaScripttheory.stanford.edu/~ataly/Talks/OperSem-cs242Lec.pdf ·  · 2009-02-12Informal and Formal Semantics Informal semantics: Meaning

Need for a Formal Semantics ? Structural Operational Semantics for IMP Structural Operational Semantics for JavaScript Applications of the Operational Semantics for JavaScript Conclusions and Future work

Thank You !

Ankur Taly A Structural Operational Semantics for JavaScript