Joe-E: A Security-Oriented Subset of Java

Joe-E: A Security-Oriented Subset of Java

Adrian Mettler David Wagner University of California, Berkeley { amettler, daw} @cs.berkeley.edu

Tyler Close waterken.org tyler.close@gmail.com

Abstract

We present Joe-E, a language designed to support the development of secure software systems. Joe-E is a subset of Java that makes it easier to architect and implement pro grams with strong security properties that can be checked during a security review. It enables programmers to ap ply the principle of least privilege to their programs; imple ment application-specific reference monitors that cannot be bypassed; introduce and use domain-specific security ab stractions; safely execute and interact with untrusted code; and build secure, extensible systems. Joe-E demonstrates how it is possible to achieve the strong security properties of an object-capability language while retaining the fea tures and feel of a mainstream object-oriented language. Additionally, we present ways in which Java’s static type safety complements object-capability analysis and permits additional security properties to be verified statically, com pared with previous object-capability languages which rely on runtime checks. In this paper, we describe the design and implementation of Joe-E and its advantages for secu rity and auditability over standard Java. We demonstrate how Joe-E can be used to develop systems with novel secu rity properties that would be difficult or impossible to en sure otherwise, including a web application platform that provides transparent, transactional object persistence and can safely host multiple mutually-distrustful applications in a single JVM.

1 Introduction

This paper describes the design and implementation of a programming language, called Joe-E, which supports de velopment of secure systems. Joe-E improves upon today’s languages in two important dimensions. First, Joe-E makes software more robust by reducing the number and impact of inadvertent bugs and security vulnerabilities in benign software. Second, Joe-E provides flexible mechanisms to reduce a program’s vulnerability to software components and allow the safe usage of untrusted code. Both charac teristics help to make code more amenable to code review and security audits, an important property when we place trust in computer systems’ correct operation and attack re silience. In particular, Joe-E supports construction of sys tems following a “secure by design” approach, where se curity is designed in from the start, as well as “design for review”, where the code is architected and written specif ically to make it as easy as possible for code reviewers to verify that the application meets its security goals. Joe-E is based upon the Java programming language. We show that a relatively small number of simple restric tions suffice to define a subset of Java that provides the se curity properties of an object-capability language. (In an object-capability language, all program state is contained in objects that cannot be read or written without a reference, which serves as an unforgeable capability. All external re sources are also represented as objects. Objects encapsulate their internal state, providing reference holders access only through prescribed interfaces.) A major contribution of our work is that we bring the security benefits of object-capability systems to a popular language. Additionally, we show how Java’s static type sys tem can be used to simplify the assurance of security prop erties statically, as opposed to via runtime checks used by the dynamically-typed object-capability languages found in prior work. Memory-safe languages like Java make it much easier to design robust systems and reason about their security properties than non-memory-safe languages, but in Java it is still difficult to reason about higher-level security proper ties, particularly when composing code with varying levels of trust or when auditing the security of a program. With Joe-E we are able to support richer ways of combining code entrusted to varying degrees while reviewably maintaining security properties.

Providing secure encapsulation. Consider Fig. 1, which illustrates how one might build an append-only log facility. Provided that the rest of the program is written in Joe-E, a code reviewer can be confident that log entries can only be added, and cannot be modified or removed. This review public final class Log { private final StringBuilder content; public Log() { content = new StringBuilder();

} public void write(String s) { content.append(s);

} }

Figure 1. An append-only logging facility.

is practical because it requires only inspection of the Log class, and does not require review of any other code. Con sequently, verifying this property requires only local rea soning about the logging code. Perhaps surprisingly, Java does not support this kind of local reasoning. Because Java allows the definition of na tive methods which can have arbitrary behavior and violate Java’s safety properties, all bets are off unless one is sure that the program does not use any such methods. Even if the program uses no native methods, the append-only prop erty of the above code is not guaranteed. Java’s reflection framework includes the ability to ignore the visibility spec ifier on a field, which would allow a reference-holder of the Log object to retrieve the StringBuilder contained within as if its field were declared to be public. This would violate the append-only property, as it would then be possi ble to perform arbitrary operations on the StringBuilder. While we might intuitively expect that the rest of the pro gram would be unlikely to exploit these weaknesses, we would have to read all of the code of the entire application to be sure. Joe-E removes these and other encapsulation-breaking features from Java in order to support building and reason ing about secure systems. This makes building sound, self contained application reference monitors possible. Because these reference monitors are written as part of the appli cation software itself, this provides a powerful mechanism for enforcing security policies: the programmer has the full power of the Joe-E programming language for expressing these security properties, and does not need to learn a new security specification language to specify them. We antic ipate that this will aid developers in implementing custom security abstractions.

Capabilities and least privilege. In the example above, only the parts of the program that have access to an instance of the log object will be able to add log entries; the rest of the program will be unable to affect that log instance. In particular, a reference to a Log object is a capability to ap pend entries to that log. We can control which parts of the program receive the power to append to the log by control ling who receives a reference to the log object. The rules for propagation of these capabilities are exactly the rules for propagation of references in a type-safe language, which should already be familiar to the programmer; we expect this will make it easier for programmers to reason about ca pability propagation. For instance, we might have an application where it is critical that every incoming network request be logged. We could provide the component that dispatches incoming re quests a capability to the log, so it can log every incoming request. By examining the dispatcher component, we can verify that every incoming request is logged using only lo cal reasoning. If required, we could also verify that no other log entries are added, by checking that no other component can receive a reference to the log. Capabilities also support least privilege. Code can only write to the log if it has a capability to the log object. Code that is not explicitly passed this capability has no access to it, which means that by default the overwhelming majority of code is verifiably incapable of writing to the log. Our experience is that this encourages a style of programming where only the code that legitimately needs the power to append to the log receives a capability to do so. Analysis of who has access to an object and the principle of least privilege are both subverted when capabilities are stored in global variables and thus are potentially readable by any part of the program. Once an object is globally avail able, it is no longer possible to limit the scope of analysis: access to the object is a privilege that cannot be withheld from any code in the program. Joe-E avoids these problems by verifying that the global scope contains no capabilities, only immutable data. The Java standard library also provides a large number of capabilities to all Java code, for example, the ability to write to any file that the JVM has access to. In the context of our example, this would include the file where the log is ultimately output. For this reason, Joe-E allows access to only a safe subset of the standard Java libraries.

Untrusted code and extensibility. Joe-E also allows ap plications to safely execute and interact with untrusted code. This safety is a result of the fact that Joe-E objects spring to life with no capabilities other than the ones passed to them when they were constructed. They can only acquire additional capabilities that they are explicitly passed. As a result, Joe-E is well suited to execution of untrusted code, since untrusted code written in Joe-E cannot harm anyone if it is not passed any dangerous capabilities. Partially trusted code can be granted only capabilities appropriate to its func tion and the level of trust placed in it. This aspect of Joe-E provides support for secure exten sibility. For instance, consider a graphics viewer program public interface Decoder extends Immutable { /** Returns a bitmap; retval[x][y][c] is the value at position (x,y) of color channel c. */ byte[][][] decode(byte[] imagedata);

}

Figure 2. An untrusted image decoder might implement this interface.

that can be extended with plugins for various file formats. We’d like to be able to download a plugin that interprets new image files, without exposing ourselves to attack from ma licious code. We want to ensure that the worst a malicious plugin could do is incorrectly decode an image, but for in stance it must not be able to send network packets, write to the filesystem, or interfere with decoding of other images. In Joe-E, we could enforce this by requiring plugins to be written in Joe-E and to implement the interface in Fig. 2. For instance, a JPEG decoder could implement this inter face, interpreting the data passed to it as a JPEG image and converting the result to a bitmap to be displayed. If the plu gin is only invoked through this interface, Joe-E guarantees the following remarkable security property: multiple invo cations of this method will be independent, and no state can be retained or leaked from invocation to invocation. This ensures both confidentiality (because information about a confidential image cannot leak into other images, even if the plugin is buggy) as well as integrity (even if the plugin contains bugs that can be exploited, say, by a maliciously constructed image, these exploits cannot interfere with the decoding of other images or otherwise harm the rest of the system, except by decoding the malicious image to an un expected bitmap). The Immutable interface, defined by the Joe-E library, is treated specially by the language: the Joe-E verifier checks that every object implementing this interface will be (deeply) immutable, and raises a compile-time error if this cannot be automatically verified. Since the Decoder interface extends Immutable, decoding plugins will neces sarily be stateless. Also, because only byte arrays can flow across this interface, it is easy to verify (thanks to the static type system) that plugins will never receive a capability that allows them to interact with any other system component.

Reviewable, rich behavioral properties. Joe-E can be used to enforce rich, application-specific behavioral secu rity properties. Fig. 3 defines a currency system. If used, for instance, in an online game, it would be easy to ver ify that trades between players cannot generate money from nothing. A Currency object provides the power to mint new money in the corresponding currency; it is impossible to do so without a reference to this object. A Purse can be public final class Currency { } public final class Purse { private final Currency currency; private long balance; /** Create a new purse with newly minted money, given the Currency capability. */ public Purse(Currency currency, long balance) { this.currency = currency; this.balance = balance;

} /** Create an empty purse with the same currency as an existing purse. */ public Purse(Purse p) { currency = p.currency; balance = 0;

} /** Transfer money into this purse from another. */ public void takeFrom(Purse src, long amount) { if (currency != src.currency || amount < 0 || amount > src.balance || amount + balance < 0) { throw new IllegalArgumentException();

} src.balance -= amount; balance += amount;

} public long getBalance() { return balance;

} }

Figure 3. A secure abstraction that supports flexible use of currencies.

used to hold and transfer money in a particular currency, but does not grant the power to mint new money. Note that this API is general enough to support multi ple currencies, and can easily be audited for correctness, even in the presence of multiple mutually-distrusting and potentially malicious clients. In particular, to verify that the currency API cannot be abused, one only need exam ine the code of the Currency and Purse classes—nothing more. From this code we can deduce, for instance, that it is only possible to create money in a currency if one has a reference to the corresponding Currency object. This kind of local reasoning is made possible because Joe-E enforces encapsulation boundaries that follow the program’s lexical scoping structure. Joe-E enables us to concentrate trust in a small, com prehensively reviewable portion of the code, which serves as the trusted computing base (TCB) for a specific secu rity property. Here the Purse only needs to be trusted to correctly enforce the security properties associated with the currency, and not for other purposes. This pattern encourages architecting a program so that for each desired secu rity property, we can identify a small TCB for that property. Such a software architecture can, in turn, significantly re duce the cost of verifying security properties of the applica tion.

This paper. In the rest of this paper, we describe the goals we had for the Joe-E language (§ 2), the design of the lan guage to meet these goals (§ 3–4), patterns that can be used in reasoning about security of Joe-E applications (§ 5), and our implementation of the Joe-E language and runtime (§ 6). Finally, we evaluate the degree to which Joe-E has met our goals by discussing our experience with the Waterken server and how it makes use of Joe-E to host mutually-distrustful web applications (§ 7).

2 Goals and Overview

We have three primary design goals for the Joe-E lan guage. First, we want Joe-E to be usable by programmers. Second, we want Joe-E to support construction of secure systems. Third, we want to make it easier to verify that the resulting systems meet their security requirements, and ease the task of security code reviews. We elaborate on these goals below, and sketch Joe-E’s approach to each of those goals.

2.1 Ease of use

To minimize barriers to adoption of Joe-E and reduce the learning curve for new Joe-E programmers, the language should be as familiar as possible for programmers. Joe E should minimize as much as possible the requirement to learn new concepts or idiosyncratic syntax. To address this goal, the Joe-E programming language is based upon Java (§ 3.1). Also, as much as possible, Joe-E programmers should be able to use existing development tools, build on existing li braries, and integrate with legacy systems. We partially sup port this goal by designing Joe-E as a subset of Java (§ 3.1) and exposing a capability-secure subset of the Java class li braries (§ 4.2). Joe-E should support construction of new modules, writ ten from scratch with security and Joe-E in mind. To receive the full benefits of Joe-E, software must be structured in a way that is compatible with good capability design princi ples. We do not aim to add security to existing Java code. Legacy Java code will most likely not be valid Joe-E, and even if it were, legacy code often fails to be structured in a way that respects capability principles. It is explicitly not a goal of this work to make it easy to transform arbitrary existing Java code into Joe-E; Joe-E is intended for newly written code.

While existing Java code may not transform easily to Joe-E, Java code can easily make use of modules written in Joe-E. For example, an existing Java application may add support for plugins implemented in Joe-E, thereby limiting the damage that plugin authors can cause to the main appli cation. Similarly, a large Java application may be incremen tally migrated to Joe-E by rewriting its component modules. Because any Java component of a combined application is unrestricted in privilege, it must be considered part of the trusted computing base. This Java component has the po tential to, via the abstraction-breaking features of Java, vi olate the security properties of Joe-E code, and so requires the same level of careful review required for an all-Java ap plication. Use of Joe-E components neither facilitates nor complicates review of Java code; the benefit is a reduction of the amount of Java code to be reviewed. Additionally, we desire Joe-E to have the expressivity and scalability to support large, real-world systems. We do not want our abstractions or implementation to place restric tions on the scale or complexity of applications that can be written in the language.

2.2 Supporting secure software

To facilitate construction of secure systems, Joe-E should: 1. Encourage least privilege. Joe-E is intended to help programmers achieve the principle of least privilege, at a fine level of granularity in their program, so that each subsystem, module, and object receives only the minimum privilege it needs to accomplish its task. Joe E should minimize barriers to least-privilege design of software. Joe-E supports this goal through safe defaults: by de fault, each block of code has no privileges to access system resources, and can acquire such privilege only if some other entity passes it an appropriate capability (§ 3). In comparison, the default in most other software platforms is that code runs with all of the privileges of the user who invoked it, and must explicitly drop privileges if that is desired; Joe-E reverses this pre sumption (§ 4.2). Joe-E’s libraries provide a capability interface to system resources (e.g., the filesystem and network). Also, applications written in Joe-E can de vise their own security abstractions that divide up priv ileges into smaller pieces appropriate to the application domain (§ 5.5), further supporting least-privilege pro gramming. We expect that systems built in this way will be more robustly secure, because the effect of bugs and vulnerabilities is limited: the fewer privileges a component has, the less harm it can do if it misbehaves or runs amok.

  1. Isolate untrusted code. We want programs to be able to run untrusted or mobile code safely. Moreover, we want programs to be able to interact usefully and effi ciently with the untrusted code—and in particular, we want to be able to run untrusted code in the same JVM as trusted code. This implies that simple isolation is not enough; programs must be able to “poke holes in the sandbox” to enable controlled sharing. We would like the trusted program and untrusted program to be able to share access to common data structures, and we want cross-domain calls to be as efficient as a method call. Because Joe-E code receives, by default, no capabili ties, it is safe to execute untrusted code that is written in Joe-E (§ 3). We can limit what the untrusted code can do, by limiting what capabilities we provide to it; and conversely, we can grant the untrusted code lim ited powers by passing it appropriate capabilities. For instance, we can enable the untrusted code to write to a single file on the filesystem, by passing it a capabil ity for that file. In Joe-E, data structures can be shared between components simply by passing a reference to the data structure, and cross-domain calls are a method call. 3. Enable safe cooperation. As a generalization of the previous point, we also want to enable mutually dis trusting subsystems to interact safely. Each party should be able to limit its exposure, should the counter party be malicious. Joe-E helps with this goal by sup porting strong encapsulation, down to the object gran ularity. Each object can be written to enforce its in variants while protecting itself from code that makes use of it (§ 5.3).
2.3 Supporting security code review

Joe-E should help programmers follow a “design for re view” philosophy, where the software architecture and im plementation are carefully chosen to facilitate security code review. Joe-E should: 1. Enable reasoning about privileges. It is not enough for Joe-E to enable least privilege and isolation; it should also be feasible for reviewers to verify that these se curity goals are achieved. Accordingly, Joe-E should help reviewers upper-bound the set of capabilities a particular block of code might ever gain access to, or upper-bound the portions of the program that might ever gain access to a particular capability. Joe-E should also make it possible to write code so that these upper bounds are precise and easily verifiable. To help with this, Joe-E is designed to enable several powerful pat

terns of reasoning about the flow of capabilities in the program (§ 5). 2. Support modular reasoning. Joe-E should make it eas ier to reason about security properties. If the program is written appropriately, it should be feasible to ver ify a security property by examining a small fraction of the code. If the object O implements some secu rity abstraction, it should be possible to reason about the security properties of this abstraction (e.g., the in variants maintained by O) just by looking at the source code for O and the objects O relies upon. In particular, if client objects C1, … , Cn make use of O, we should be able to verify the correctness of O without exam ining the code of any client Ci. We call this modular analysis. Modular analysis is critical if security code review is to scale to large programs. Joe-E’s strategy for supporting modular reasoning about security relies heavily on flexible support for iso lation of untrusted code (§ 5.3). Also, many of our restrictions on Joe-E code support modular reasoning: the more we restrict what Joe-E code can do, the more we can restrict the possible behaviors of each client Ci, which makes it easier to ensure that they do not violate O’s invariants. 3. Support reasoning about mutability. Shared mutable state is a headache for reasoning about security, be cause it introduces the potential for race conditions, time-of-check-to-time-of-use vulnerabilities, and sur prising consequences of aliasing. Joe-E should help programmers avoid these risks by providing first-class support for reasoning about mutability and immutabil ity. In particular, Joe-E should make it easy for pro grammers to build data structures that are transitively immutable, should provide support for static verifica tion of this fact, and should reflect these immutability properties in the static type system. Joe-E addresses this by extending the Java type system with immutabil ity annotations (§ 5.4), by providing library support for programming with immutable data, and by forbidding mutable global variables (§ 4.2, § 5.2).

3 Approach

Our approach to improving language security is through the use of an object-capability language. Such languages permit a default-deny, least-privilege approach to the au thority granted to parts of a program as it executes. The central feature of object-capability languages is that they use object references (pointers to objects) to represent all of the privileges that can be used by a program. In the simplest case, these simply point to encapsulated memory resident objects. Having a pointer to such an object grants the ability to interact with it via its public interface. Since access to the object is limited to the interface, the object can be designed to maintain the privacy and integrity of its internal state even when passed to untrusted code. For many purposes, a system that can only operate on in-memory objects is not enough. Most programs need to interface with other resources on the system or network. In object-capability languages, these resources are represented as objects defined by special library classes. Reference to such an object allows interaction with the external resource via a library-defined public interface. In this way, files on disk and network connections are naturally represented as objects. Access to all references in an object-capability language is governed by program scope. At any point in time, the pro gram can only make use of the capabilities that are reach able from its in-scope references. For such an approach to be sound, the language must be memory-safe: it must be impossible to “forge” a pointer to an object, such as by per forming a type cast operation on a memory address. To get the most benefit from this approach, we want the minimal set of privileges we can bestow on part of a pro gram to be as small as possible. We’d like the “default state” for running code to be one in which no harm can be done unless we explicitly trust it with a reference. For this to be the case, the global scope (which is available everywhere in the program) should not allow access to any authority we would want to deny to completely-untrusted code. Ideally, we want code to be unable to do anything unless we have granted it a capability to do so. In real systems, we may need to relax this slightly for practical reasons; it may be easy to limit access to in-memory objects and external resources, but too difficult to prevent code from consuming CPU cycles or memory, or failing to return in a timely man ner. Our approach is to place no limits on the purely com putational power of untrusted code, limiting only its access to data and external resources. If the global scope grants no access to privileges of concern, one can enforce least priv ilege on a fine-grained basis by ensuring that each scope in the program’s execution only has access to the capabilities it needs. More importantly, it is possible to reason about the authority with which different parts of the program are trusted. Every component of the program has only the ca pabilities that have been passed to it. In contrast with most other object-capability languages, which use dynamic typing, in Joe-E we can leverage the Java type system to place static restrictions on how capa bilities can propagate as a program executes. With this ap proach we are able to restrict the flow of capabilities while reducing the need for reference monitors and explicit dy namic checks in order to guarantee security properties.

3.1 Subsetting

Many new languages have been proposed over the years, but relatively few have seen widespread adoption. Program mers have large amounts of experience with and code in ex isting languages, and thus are reluctant to invest in switch ing to a new language. A number of new languages have been defined as exten sions to existing languages. This has the advantage of lever aging developer experience and preserving a greater degree of familiarity than defining a new language from scratch. Unfortunately, programs written in the extended language become incompatible with tools (debuggers, interpreters, profilers, IDEs) designed for the original language. De velopers are wary of becoming locked into such extended languages, as they are not guaranteed to maintain the same level of support as the base language going forward. We take a different approach: we define the Joe-E lan guage as a subset of Java. Every Joe-E program is simply a Java program that satisfies additional language restrictions that are verified by the Joe-E verifier. We avoid adding new features to Java or making changes to Java’s semantics; in stead, we impose restrictions on the source code that ev ery valid Joe-E program must satisfy (see Fig. 4 and § 4). The Joe-E verifier checks that these restrictions are met, but not does not transform the program in any way. This ap proach allows use of the standard Java tools, compiler, and runtime, as well as allowing Joe-E programs to coexist with Java code and libraries. 1 More importantly, this allows us to leverage programmers’ experience with the Java language, while introducing security-oriented programming patterns. Joe-E can be thought of as simply an idiomatic way to write Java code, using conventions that facilitate a style of rea soning. The Joe-E verifier ensures that all checked code conforms to these conventions.

4 Design of Joe-E

The Joe-E language restrictions are chosen so it will be intuitive and predictable to the programmer which pro grams will pass the Joe-E verifier. We avoid sophisticated program analysis, instead favoring programming rules that are simple to state. For similar reasons, we avoid whole program analysis. Instead, the Joe-E verifier analyzes each source file individually. This file-at-a-time approach also helps scalability and lets us support open-world extensibil ity: new code can be added to the