Add NVRAM size limit to nano-emulator and add test to recover from NVRAM hog attack.

Change-Id: I58265ddf26f2e93b9057fe6b95fb3c1b98e82e99

Add NVRAM-hogging DOS attack.

Change-Id: Ia178e42539a771747ab8a96560eb2d374ed07904

BUG=none
TEST=passes included test

Review URL: http://codereview.chromium.org/4183005
diff --git a/utility/chromeos_tpm_recovery b/utility/chromeos_tpm_recovery
index 15b765c..cbafce4 100755
--- a/utility/chromeos_tpm_recovery
+++ b/utility/chromeos_tpm_recovery
@@ -72,9 +72,10 @@
   tpm_unowned=1
 }
 
+# We want the TPM owned with the well-known password.
+
 ensure_tpm_is_owned () {
-  if [ $tpm_owned_with_well_known_password = 0 -a \
-       $tpm_unowned = 0 ]; then
+  if [ $tpm_owned_with_well_known_password = 0 ]; then
     tpm_clear_and_reenable
     ensure_tcsd_is_running
     $tpm_takeownership -y -z || log "takeownership failed with status $?"
@@ -116,16 +117,20 @@
 /# NV Index / { print $4 } #unexpected space
 EOF
 
+  local index
+
+  log "trying to make room by freeing one space"
   ensure_tcsd_is_running
   ensure_tpm_is_owned
   unexpected_spaces=$($nvtool --list | $awk -f $AWK_PROGRAM)
 
   status=1
 
-  if ("$unexpected_spaces" != ""); then
+  if [ "$unexpected_spaces" != "" ]; then
     log_tryfix "unexpected spaces: $unexpected_spaces"
     for index in $unexpected_spaces; do
-      if remove_space $index; then
+      log "trying to remove space $index"
+      if remove_space $(printf "0x%x" $(( $index )) ); then
         status=0
         break;
       fi
@@ -148,10 +153,13 @@
 
   ensure_tpm_is_unowned
   while true; do
+    log "checking for NVRAM room for space with size $size"
     if $tpmc definespace $test_space $size $perm_ppwrite; then
+      log "there is enough room"
       enough_room=1
       break
     else
+      log "definespace $test_space $size failed with status $?"
       if ! make_room; then
         enough_room=0
         break
@@ -228,7 +236,7 @@
 if [ $# = 1 ]; then
   RECOVERY_LOG="$1"
   /usr/bin/logger "$0 started, output in $RECOVERY_LOG"
-  log "starting"
+  log "starting $0"
 else
   /usr/bin/logger "$0 usage error"
   echo "usage: $0 <log file>"