Blame SOURCES/rhbz1643997.0026-PR23860-additional-ugly-stack-clobber-protection-for.patch

132810
From a7605f298f8d823429b8cfd264bd28c3bd345eb5 Mon Sep 17 00:00:00 2001
132810
From: Serhei Makarov <smakarov@redhat.com>
132810
Date: Fri, 9 Nov 2018 14:36:19 -0500
132810
Subject: [PATCH 26/32] PR23860: additional ugly stack/clobber protection for
132810
 strings
132810
132810
In addition to prior commit, emit_string_copy() does not work for
132810
overlapping source/destination. So, make sure strings are not
132810
allocated in a way which overlaps map key/value arguments.
132810
132810
This increases space pressure, inducing a couple of bpf-asm.exp
132810
testcase failures.
132810
132810
* bpf-internal.h (value::format_str): New flag.
132810
(value::value): Take format_str flag.
132810
(value::mk_str): Take format_str flag.
132810
(program::format_map): New field, caches format_str separately.
132810
(program::new_str): Take format_str flag.
132810
* bpf-base.cxx (program::new_str): Cache format_str separately.
132810
* bpf-opt.cxx (alloc_literal_str): Store non-format str in lower half.
132810
* bpf-translate.cxx (emit_string_copy): Comment -- doesn't support overlap.
132810
(emit_string_copy): DEBUG_CODEGEN -- identify if zero-padding was done.
132810
(emit_print_format): Set format_str flag.
132810
---
132810
 bpf-base.cxx      | 13 ++++++++-----
132810
 bpf-internal.h    | 15 +++++++++++----
132810
 bpf-opt.cxx       | 35 ++++++++++++++++++++++++-----------
132810
 bpf-translate.cxx |  7 +++++--
132810
 4 files changed, 48 insertions(+), 22 deletions(-)
132810
132810
diff --git a/bpf-base.cxx b/bpf-base.cxx
132810
index 5d132bcd1..b19b69133 100644
132810
--- a/bpf-base.cxx
132810
+++ b/bpf-base.cxx
132810
@@ -657,14 +657,17 @@ program::new_imm(int64_t i)
132810
 }
132810
 
132810
 value *
132810
-program::new_str(std::string str)
132810
+program::new_str(std::string str, bool format_str)
132810
 {
132810
-  auto old = str_map.find(str);
132810
-  if (old != str_map.end())
132810
+  std::unordered_map<std::string, value *>& m = str_map;
132810
+  if (format_str) m = format_map;
132810
+
132810
+  auto old = m.find(str);
132810
+  if (old != m.end())
132810
     return old->second;
132810
 
132810
-  value *v = new value(value::mk_str(str));
132810
-  auto ok = str_map.insert(std::pair<std::string, value *>(str, v));
132810
+  value *v = new value(value::mk_str(str, format_str));
132810
+  auto ok = m.insert(std::pair<std::string, value *>(str, v));
132810
   assert(ok.second);
132810
   return v;
132810
 }
132810
diff --git a/bpf-internal.h b/bpf-internal.h
132810
index 75fefb769..f97501d7d 100644
132810
--- a/bpf-internal.h
132810
+++ b/bpf-internal.h
132810
@@ -78,18 +78,24 @@ struct value
132810
   int64_t imm_val;
132810
   std::string str_val;
132810
 
132810
-  value(value_type t = UNINIT, regno r = noreg, int64_t c = 0, std::string s = "")
132810
-    : type(t), reg_val(r), imm_val(c), str_val(s)
132810
+  bool format_str; // for str_val
132810
+
132810
+  value(value_type t = UNINIT, regno r = noreg, int64_t c = 0,
132810
+        std::string s = "", bool format_str = false)
132810
+  : type(t), reg_val(r), imm_val(c), str_val(s), format_str(format_str)
132810
   { }
132810
 
132810
   static value mk_imm(int64_t i) { return value(IMM, noreg, i); }
132810
-  static value mk_str(std::string s) { return value(STR, noreg, 0, s); }
132810
+  static value mk_str(std::string s, bool format_str = false) {
132810
+    return value(STR, noreg, 0, s, format_str);
132810
+  }
132810
   static value mk_reg(regno r) { return value(TMPREG, r); }
132810
   static value mk_hardreg(regno r) { return value(HARDREG, r); }
132810
 
132810
   bool is_reg() const { return type >= HARDREG; }
132810
   bool is_imm() const { return type == IMM; }
132810
   bool is_str() const { return type == STR; }
132810
+  bool is_format() const { assert(is_str()); return format_str; }
132810
 
132810
   regno reg() const { assert(is_reg()); return reg_val; }
132810
   int64_t imm() const { assert(is_imm()); return imm_val; }
132810
@@ -262,12 +268,13 @@ struct program
132810
   // Store at most one of each IMM and STR value:
132810
   std::unordered_map<int64_t, value *> imm_map;
132810
   std::unordered_map<std::string, value *> str_map;
132810
+  std::unordered_map<std::string, value *> format_map;
132810
 
132810
   regno max_reg() const { return reg_vals.size() + MAX_BPF_REG; }
132810
   value *lookup_reg(regno r);
132810
   value *new_reg();
132810
   value *new_imm(int64_t);
132810
-  value *new_str(std::string);
132810
+  value *new_str(std::string, bool format_str = false);
132810
 
132810
   // The BPF local stack is [0, -512] indexed off BPF_REG_10.
132810
   // The translator has dibs on the low bytes, [0, -max_tmp_space],
132810
diff --git a/bpf-opt.cxx b/bpf-opt.cxx
132810
index 089dcde55..f3aa5c462 100644
132810
--- a/bpf-opt.cxx
132810
+++ b/bpf-opt.cxx
132810
@@ -19,38 +19,51 @@ namespace bpf {
132810
 
132810
 // Allocate space on the stack and store a string literal in that space:
132810
 static value *
132810
-alloc_literal_str(program &p, insn_inserter &ins, std::string &str)
132810
+alloc_literal_str(program &p, insn_inserter &ins, value *s)
132810
 {
132810
+  std::string str = s->str();
132810
+
132810
+  size_t str_bytes = str.size() + 1;
132810
+  str_bytes += 4 - str_bytes % 4; // write aligned words to avoid garbage data
132810
+
132810
+  int ofs; size_t tmp_space;
132810
+
132810
   // Append the string to existing temporary data.
132810
   //
132810
   // TODO: This could produce significant space limitations.
132810
   // A better solution would be to integrate with the
132810
   // register allocator and reclaim the space after
132810
   // the string literal is no longer live.
132810
-  size_t tmp_space = p.max_tmp_space;
132810
+  tmp_space = p.max_tmp_space;
132810
   tmp_space += 4 - tmp_space % 4; // write aligned words to avoid verifier error
132810
   p.use_tmp_space(tmp_space);
132810
 
132810
-  size_t str_bytes = str.size() + 1;
132810
-  str_bytes += 4 - str_bytes % 4; // write aligned words to avoid garbage data
132810
   if (tmp_space + str_bytes > MAX_BPF_STACK)
132810
     throw std::runtime_error("string allocation failed due to lack of room on stack");
132810
 
132810
   tmp_space += str_bytes;
132810
 
132810
 #if 1
132810
+  // The following aren't ideal because an unlucky ordering of
132810
+  // allocation requests will waste additional space.
132810
+
132810
   // XXX PR23860: Passing a short (non-padded) string constant can fail
132810
   // the verifier, which is not smart enough to determine that accesses
132810
   // past the end of the string will never occur. To fix this, make sure
132810
   // the string offset is at least -BPF_MAXSTRINGLEN.
132810
-  //
132810
-  // Not ideal because an unlucky ordering of allocations may waste space.
132810
-  if (tmp_space < BPF_MAXSTRINGLEN)
132810
-    tmp_space = BPF_MAXSTRINGLEN;
132810
+  //if (!s->is_format() && tmp_space < BPF_MAXSTRINGLEN)
132810
+  //  tmp_space = BPF_MAXSTRINGLEN;
132810
+
132810
+  // TODO PR23860: An even uglier workaround for emit_string_copy()
132810
+  // overlapping source and destination regions. Only do this for
132810
+  // non-format strings, as format strings are not manipulated by the
132810
+  // eBPF program.
132810
+  if (!s->is_format() && tmp_space < BPF_MAXSTRINGLEN * 2 + str_bytes)
132810
+    tmp_space = BPF_MAXSTRINGLEN * 2 + str_bytes;
132810
 #endif
132810
 
132810
   p.use_tmp_space(tmp_space);
132810
-  int ofs = -tmp_space;
132810
+  ofs = -tmp_space;
132810
 
132810
   value *frame = p.lookup_reg(BPF_REG_10);
132810
   value *out = emit_simple_literal_str(p, ins, frame, ofs, str, false /* don't zero pad */);
132810
@@ -73,7 +86,7 @@ lower_str_values(program &p)
132810
             {
132810
               insn_before_inserter ins(b, j, "str");
132810
               std::string str0 = s0->str();
132810
-              value *new_s0 = alloc_literal_str(p, ins, str0);
132810
+              value *new_s0 = alloc_literal_str(p, ins, s0);
132810
               j->src0 = new_s0;
132810
             }
132810
 
132810
@@ -82,7 +95,7 @@ lower_str_values(program &p)
132810
             {
132810
               insn_before_inserter ins(b, j, "str");
132810
               std::string str1 = s1->str();
132810
-              value *new_s1 = alloc_literal_str(p, ins, str1);
132810
+              value *new_s1 = alloc_literal_str(p, ins, s1);
132810
               j->src1 = new_s1;
132810
             }
132810
         }
132810
diff --git a/bpf-translate.cxx b/bpf-translate.cxx
132810
index 0181380b7..20cd47032 100644
132810
--- a/bpf-translate.cxx
132810
+++ b/bpf-translate.cxx
132810
@@ -2568,6 +2568,9 @@ emit_simple_literal_str(program &this_prog, insn_inserter &this_ins,
132810
 // dest[+ofs] in 4-byte chunks, with optional zero-padding up to
132810
 // BPF_MAXSTRINGLEN.
132810
 //
132810
+// TODO (PR23860): This code does not work when the source and target
132810
+// regions overlap.
132810
+//
132810
 // ??? Could use 8-byte chunks if we're starved for instruction count.
132810
 // ??? Endianness of the target may come into play here.
132810
 value *
132810
@@ -2583,7 +2586,7 @@ bpf_unparser::emit_string_copy(value *dest, int ofs, value *src, bool zero_pad)
132810
     }
132810
 
132810
 #ifdef DEBUG_CODEGEN
132810
-  this_ins.notes.push("strcpy");
132810
+  this_ins.notes.push(zero_pad ? "strcpy_zero_pad" : "strcpy");
132810
 #endif
132810
 
132810
   size_t str_bytes = BPF_MAXSTRINGLEN;
132810
@@ -2866,7 +2869,7 @@ bpf_unparser::emit_print_format (const std::string& format,
132810
   // bpf program stack.  This is handled by bpf-opt.cxx lowering STR values.
132810
   size_t format_bytes = format.size() + 1;
132810
   this_prog.mk_mov(this_ins, this_prog.lookup_reg(BPF_REG_1),
132810
-                   this_prog.new_str(format));
132810
+                   this_prog.new_str(format, true /*format_str*/));
132810
   emit_mov(this_prog.lookup_reg(BPF_REG_2), this_prog.new_imm(format_bytes));
132810
   for (size_t i = 0; i < nargs; ++i)
132810
     emit_mov(this_prog.lookup_reg(BPF_REG_3 + i), actual[i]);
132810
-- 
132810
2.14.5
132810