<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="Author" CONTENT="Mark S. Miller">
   <META NAME="GENERATOR" CONTENT="Mozilla/4.01 [en] (Win95; I) [Netscape]">
   <TITLE>The Grant Matcher Puzzle</TITLE>
</HEAD>
<BODY BACKGROUND="wood.gif">

<CENTER>
<H1>
Identity Untangled:</H1></CENTER>

<CENTER>
<H1>
The <I>Grant Matcher</I> Puzzle</H1></CENTER>

<CENTER>by Mark Miller,</CENTER>

<CENTER>with many thanks to Norm Hardy and E. Dean Tribble</CENTER>

<CENTER>
<HR WIDTH="100%"></CENTER>


<P>Many systems designers have wrestled with the notion of object identity.
The issues must be resolved to design foundational equality primitives.
Should an object system provide a means to tell whether two object references
refer to the same object, without consulting either of the objects involved?
Following Lisp, we call any such primitive <I>EQ</I>. Pure capability systems
that are otherwise equivalent have come to different answers. The implications
of these different answers were <A HREF="history.html">not understood</A>
until the Grant Matcher Puzzle.

<P>
<HR WIDTH="100%">
<CENTER>
<H2>
<IMG SRC="capability2.gif" HEIGHT=271 WIDTH=212 ALIGN=RIGHT>Capability
Foundations</H2></CENTER>
Shown here is the fundamental step of capability computation. Alice, Bob,
and Carol are three separate objects. In the initial conditions Alice holds
an object reference--or <I>capability</I>--to both Bob and Carol, and neither
hold a capability to the other two.
<UL>
<LI>
The first rule of capabilities is that one object, here, Bob, can only
get a access to another object, here, Carol, if the first is creates the
second (not shown), or if someone, here, Alice, who validly has access
to the Carol voluntarily passes a copy of this access to Bob. In order
for Alice to give Bob access to Carol, Alice must already have access to
Carol, and Alice must choose to give out this access. What does it mean
for code to voluntarily choose? After all, the code may even execute deterministically,
so some definitions of free will wouldn't work. By <I>voluntary</I> we
mean: Were a different program substituted for Alice, but placed in the
same exact external environment, that program would be able to choose not
to give access to Carol. This is the basis for <I>discretionary security</I>
in capability systems.</LI>

<LI>
The second rule is that capabilities may only travel on paths provided
by existing capabilities. In order for Alice to give Bob access to Carol,
Alice also requires access to Bob. Even if Alice is coded to attempt to
give this access to Bob, she cannot if there is no capability pathway of
willing intermediaries leading from Alice to Bob. This is the basis for
<I>mandatory security</I> in capability systems, especially <I><A HREF="http://www.caplet.com/security/taxonomy/confinement.html">confinement</A></I>.</LI>

<LI>
The third rule is that the access to Carol that Bob gets must be as good
a reference to Carol as the reference Alice passed <I>as far as Alice is
concerned</I>. The "Carol" that Bob gets must be the "Carol" that Alice
<I>meant</I>.</LI>
</UL>
The subtleties in making this last issue precise are the heart of the Grant
Matcher Puzzle. <A HREF="distrib-grant.html">Matching Distributed Grants</A>.explains
how to avoid the capability equivalent of a <I>man in the middle attack</I>,
otherwise a danger in a distributed cryptographic capability system.
<CENTER>
<H2>
<IMG SRC="grant-matcher.gif" HEIGHT=374 WIDTH=437 ALIGN=RIGHT>Setting up
the Puzzle</H2></CENTER>
In the initial conditions of Grant Matcher Puzzle, the Grant Matcher itself
plays the role of Bob, and a charity named KEQD plays the role of Carol.
Dana's situation is exactly symmetric with Alice. Alice and Dana are both
assumed to trust the Grant Matcher to perform its duties.
<UL>Their only protection against misbehavior by the Grant Matcher is the
<I><A HREF="http://www.communities.com/company/papers/security/problems.html">principle
of least authority</A></I> (called by Saltzer and Shroeder the <I>principle
of least priviledge</I>). The Grant Matcher's protocol only requires Alice
and Dana to give the Grant Matcher those capabilities it would require
to honestly perform its duties. A protocol requiring more authority than
this should raise eyebrows. In the context of the Grant Matcher Puzzle
as posed, Alice and Bob are vulnerable to the misbehavior possible <I>within
</I>these bounds, about which we will not concern ourselves further.</UL>
Money is itself an interesting problem, but should simply be assumed for this 
puzzle. <A HREF="#(The-Money-System)">Here</A> is an implementation of money in 
Java adequate for this example, secure assuming only that one can prevent further 
entrants into a package. 
<P>Alice and Dana are assumed not to trust each other at all. That is why
they are using the Grant Matcher as a mutually trusted third party. No
system can enable cooperation in the absence of any trust. The Grant Matcher
pattern, however, is supposed to bring about a particular kind of cooperation
between Alice and Dana, requiring only that they both trust the Grant Matcher
and a common monetary system.
<CENTER>
<H2>
<IMG SRC="keqd-gets-it.gif" HEIGHT=377 WIDTH=365 ALIGN=RIGHT>When it Works</H2></CENTER>
The Grant Matcher provides the service of matching grants, to be given
to a common destination. Alice wishes to use the Grant Matcher to give
$10 to KEQD, but only if $10 also goes to KEQD from Dana. Dana has the
symmetric desire involving Alice. The Grant Matcher has no previous knowledge
of KEQD, but if both Alice and Dana provide the same amount of money and
designate the same destination, the Grant Matcher will take the money from
each and give the sum to the destination. Otherwise, the Grant Matcher
returns the money to the prospective donors.

<P>The Grant Matcher is assumed to be coded to perform its duties if it
is possible for it to do so. The puzzle is: Can the Grant Matcher determine
if Alice and Dana are designating the same destination? Having made a determination,
can the Grant Matcher reliably transport the money to the destination,
in a way mutually acceptable to Alice and Dana? The Grant Matcher must
operate so as to ensure that Alice will not lose $10 unless a destination
acceptable to her gets $20. Similarly for Dana.

<P>Let us assume there is no EQ primitive--that one can only gain information
about a capability by sending messages over it. In that case, the Grant
Matcher has to determine equality by sending messages over these capabilities
in some equality determining protocol. Having determined--somehow--that
both references are equivalent, the Grant Matcher can simply pick one and
send the money.
<BR>&nbsp;
<BR>&nbsp;
<CENTER>
<H2>
<IMG SRC="alice-steals.gif" HEIGHT=377 WIDTH=437 ALIGN=RIGHT>Alice Gets
Greedy</H2></CENTER>
In Actor systems and in Joule, these equality protocols may eventually
bottom out in an internal EQ primitive, but this primitive is hooked into
the system in such a way as to prevent revealing a transparent forwarder
interposed on a messaging path. If the primitive could be used to reveal
the forwarder, then full transparency would be lost. In such a system,
one could not have a <I>transparent</I> forwarder.

<P>So, in a system in which forwarders can truly be transparent, Alice
can send to the Grant Matcher instead a reference to a transparent forwarder
to KEQD. This forwarder always transparently sends messages through to
KEQD, unless those messages carry $20. In that latter circumstance, the
forwarder will deposit the money to Alice's bank account. By assumption,
the Grant Matcher cannot distinguish this situation from the earlier one,
except at the price of the very $20 that is at stake.

<P>Notice that Alice cannot really be said to have done anything dishonest.
The cause that she is designating to the Grant Matcher is simply one that
acts just like KEQD, except for where it puts $20 bills. If Dana were to
designate this same forwarder object in its request to the Grant Matcher,
it would be stating that this object is where it would like to see the
money go as well.

<P><IMG SRC="alice-gets-it.gif" HEIGHT=377 WIDTH=365 ALIGN=RIGHT>Since
Alice's object is forwarding messages to KEQD, including the messages that
make up the equality protocol, the intermediate object could validly also
be seen as part of the plumbing--as a message conduit for delivering messages
to KEQD. In this sense, KEQD itself would also be a valid interpretation
of <I>what Alice meant</I> by the capability she passed to the Grant Matcher.
Were the Grant Matcher to give $20 directly to KEQD instead of her forwarder,
Alice would have no grounds for complaint.

<P>However, the Grant Matcher's situation is completely symmetrical, so
it might still break the symmetry in Alice's favor, in which case Alice
pockets the money. Dana has lost his $10 even though no destination acceptable
to Dana got $20. By no stretch of semantics could one interpret Dana's
actions so as to say that Alice's bank account was a valid interpretation
of the destination Dana <I>meant </I>to designate.
<BR><BR CLEAR=BOTH>

<HR WIDTH="100%">
<CENTER>
<H2>
<IMG SRC="refund.gif" HEIGHT=319 WIDTH=388 ALIGN=RIGHT>How EQ Makes a Difference</H2></CENTER>
Of all varieties of EQ primitive I am aware of, the simplest is the originally
address equality primitive from Lisp, since then appearing in everything
from Smalltalk to KeyKOS to Java. Were the Grant Matcher to use this primitive,
EQ would say <I>false</I> in the "Alice Gets Greedy" scenario, and both
would get their money back instead. This is a perfectly acceptable outcome,
though it has some real costs, especially in the <A HREF="distrib-grant.html">distributed
case</A>.

<P>Here is an address-equality-based Grant Matcher coded in Java. Ignoring
exceptions from outside the puzzle, the following is secure assuming only
that one can prevent further entrants into a package.
<UL>The Money System
<UL>The following money system is a simplification of the <A HREF="http://www.agorics.com/joule.html#(Hierarchical-Accounts-Example)">Fractal
Reserve Bank</A>. Though the Java code here does rely on EQ, unlike the
Grant Matcher puzzle itself, the linked-to implementation in Joule demonstrates
EQ is not required for this kind of money.
<CENTER><A HREF="../../javadoc/Package-foresight.examples.money.html">Javadoc-umentation</A></CENTER>

<TABLE CELLSPACING=0 CELLPADDING=4 >
<TR>
<TD ALIGN=CENTER VALIGN=CENTER><A HREF="../../sources/foresight/examples/money/Currency.java">Currency.java</A></TD>

<TD WIDTH="100"></TD>

<TD>Each currency results from a separate Mint creation event. Money is
only primitively transferable within a currency.</TD>
</TR>

<TR>
<TD ALIGN=CENTER VALIGN=CENTER><A HREF="../../sources/foresight/examples/money/Purse.java">Purse.java</A></TD>

<TD></TD>

<TD>Money is not represented directly, but rather as counters held in Purses.
Purses of the same currency will transfer money between them, while enforcing
properties such as local conservation.</TD>
</TR>

<TR>
<TD ALIGN=CENTER VALIGN=CENTER><A HREF="../../sources/foresight/examples/money/Mint.java">Mint.java</A></TD>

<TD></TD>

<TD>The power to print more money in a given currency. This is where money
comes from. All other operations can only transfer it around.</TD>
</TR>

<TR>
<TD ALIGN=CENTER VALIGN=CENTER><A HREF="../../sources/foresight/examples/money/DifferentCurrencyException.java">DifferentCurrencyException.java</A></TD>

<TD></TD>

<TD>If an attempt is made to directly transfer between different currencies.
Note that the system allows intermediary money changers that trade in multiple
currencies.</TD>
</TR>

<TR>
<TD ALIGN=CENTER VALIGN=CENTER><A HREF="../../sources/foresight/examples/money/NegativeBalanceException.java">NegativeBalanceException.java</A></TD>

<TD></TD>

<TD>If an action would cause a balance to go negative.</TD>
</TR>
</TABLE>
&nbsp;</UL>
Grant Matching
<UL>The following code implements the Grant Matcher as a single object.
More complicated uses of the same basic pattern might make it a two facetted
object--one each for Alice and Dana. Joule, KeyKOS, and Mach provide for
multiple facets primitively. Marc Stiegler <A HREF="http://www.communities.com/company/papers/security/facade.html">explains</A>
how to implement multiple facets in languages such as Java or E using the
Facade pattern.
<CENTER><A HREF="../../javadoc/Package-foresight.examples.grantmatch.html">Javadoc-umentation</A></CENTER>

<TABLE CELLSPACING=0 CELLPADDING=4 >
<TR>
<TD ALIGN=CENTER VALIGN=CENTER><A HREF="../../sources/foresight/examples/grantmatch/Charity.java">Charity.java</A></TD>

<TD WIDTH="100"></TD>

<TD>Interface to those, like KEQD, that can accept a donation.</TD>
</TR>

<TR>
<TD ALIGN=CENTER VALIGN=CENTER><A HREF="../../sources/foresight/examples/grantmatch/GrantStatus.java">GrantStatus.java</A></TD>

<TD></TD>

<TD>Interface for callbacks to be provided by Alice and Dana in their GrantMatcher
requests so they can find out the status of their request, and receive
a refund if needed.</TD>
</TR>

<TR>
<TD ALIGN=CENTER VALIGN=CENTER><A HREF="../../sources/foresight/examples/grantmatch/GrantMatcher.java">GrantMatcher.java</A></TD>

<TD></TD>

<TD>If everything goes well, The Grant Matcher accepts two messages, escrows
the money from each, sees that they agree, gives the sum to the common
charity, and lets both sides know the transaction completed. Otherwise,
it refunds any money escrowed, and lets both sides know the transaction
failed. This class is abstract, leaving subclasses to decide how to determine
equality.</TD>
</TR>

<TR>
<TD></TD>

<TD></TD>

<TD>
<TABLE CELLSPACING=0 CELLPADDING=4 >
<TR>
<TD ALIGN=CENTER VALIGN=CENTER><A HREF="../../sources/foresight/examples/grantmatch/EQGrantMatcher.java">EQGrantMatcher.java</A></TD>

<TD WIDTH="20"></TD>

<TD>Determines equality via the Java EQ primitive, "==".</TD>
</TR>

<TR>
<TD ALIGN=CENTER VALIGN=CENTER><A HREF="../../sources/foresight/examples/grantmatch/EqualsGrantMatcher.java">EqualsGrantMatcher.java</A></TD>

<TD></TD>

<TD>Determines equality via the Java Object.equals() message.</TD>
</TR>
</TABLE>
&nbsp;</TD>
</TR>

<TR>
<TD><A HREF="../../sources/foresight/examples/grantmatch/MalletCharity.java">MalletCharity.java</A></TD>

<TD></TD>

<TD>An <I>Charity-in-the-middle</I> that forwards all equality messages
to the Charity it is trying to impersonate, while pocketing all donations.</TD>
</TR>
</TABLE>
&nbsp;</UL>
</UL>

</BODY>
</HTML>
