Blame SOURCES/Miscellaneous-fixes-1914.patch

80cbd0
From b84714a47a3a1ec646bbd489442b305c84b35e15 Mon Sep 17 00:00:00 2001
80cbd0
From: jeromemarchand <38073585+jeromemarchand@users.noreply.github.com>
80cbd0
Date: Wed, 8 Aug 2018 18:09:44 +0200
80cbd0
Subject: [PATCH] Miscellaneous fixes (#1914)
80cbd0
80cbd0
* Fix multiple memory access errors
80cbd0
80cbd0
Fixes a buffer overflow in get_pid_exe(), a use-after-free error in
80cbd0
bcc_usdt_get_probe_argctype() and a possible NULL pointer dereference
80cbd0
in find_debug_via_debuglink().
80cbd0
80cbd0
* Fix multiple ressource leaks
80cbd0
80cbd0
Leaked file descriptors in bpf_attach_uprobe() and verify_checksum().
80cbd0
Memory leaks in  Parser::func_add() and bcc_procutils_language().
80cbd0
80cbd0
* fixup! Fix multiple ressource leaks
80cbd0
---
80cbd0
 src/cc/bcc_elf.c             | 6 ++++--
80cbd0
 src/cc/bcc_proc.c            | 4 +++-
80cbd0
 src/cc/common.cc             | 2 ++
80cbd0
 src/cc/frontends/b/parser.cc | 4 +++-
80cbd0
 src/cc/libbpf.c              | 1 +
80cbd0
 src/cc/usdt/usdt.cc          | 5 +++--
80cbd0
 6 files changed, 16 insertions(+), 6 deletions(-)
80cbd0
80cbd0
diff --git a/src/cc/bcc_elf.c b/src/cc/bcc_elf.c
80cbd0
index e848912..c425db6 100644
80cbd0
--- a/src/cc/bcc_elf.c
80cbd0
+++ b/src/cc/bcc_elf.c
80cbd0
@@ -377,8 +377,10 @@ static int verify_checksum(const char *file, unsigned int crc) {
80cbd0
   if (fd < 0)
80cbd0
     return 0;
80cbd0
 
80cbd0
-  if (fstat(fd, &st) < 0)
80cbd0
+  if (fstat(fd, &st) < 0) {
80cbd0
+    close(fd);
80cbd0
     return 0;
80cbd0
+  }
80cbd0
 
80cbd0
   buf = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
80cbd0
   if (!buf) {
80cbd0
@@ -433,7 +435,7 @@ static char *find_debug_via_debuglink(Elf *e, const char *binpath,
80cbd0
 
80cbd0
 DONE:
80cbd0
   free(bindir);
80cbd0
-  if (check_crc && !verify_checksum(res, crc))
80cbd0
+  if (res && check_crc && !verify_checksum(res, crc))
80cbd0
     return NULL;
80cbd0
   return res;
80cbd0
 }
80cbd0
diff --git a/src/cc/bcc_proc.c b/src/cc/bcc_proc.c
80cbd0
index 14ee18e..6fe11a0 100644
80cbd0
--- a/src/cc/bcc_proc.c
80cbd0
+++ b/src/cc/bcc_proc.c
80cbd0
@@ -446,8 +446,10 @@ const char *bcc_procutils_language(int pid) {
80cbd0
       while (isspace(mapname[0])) mapname++;
80cbd0
       for (i = 0; i < nb_languages; i++) {
80cbd0
         snprintf(pathname, sizeof(pathname), "/lib%s", languages[i]);
80cbd0
-        if (strstr(mapname, pathname))
80cbd0
+        if (strstr(mapname, pathname)) {
80cbd0
+          fclose(procfile);
80cbd0
           return languages[i];
80cbd0
+	}
80cbd0
         if ((str = strstr(mapname, "libc")) &&
80cbd0
             (str[4] == '-' || str[4] == '.'))
80cbd0
           libc = true;
80cbd0
diff --git a/src/cc/common.cc b/src/cc/common.cc
80cbd0
index 1cfe91a..c8370a3 100644
80cbd0
--- a/src/cc/common.cc
80cbd0
+++ b/src/cc/common.cc
80cbd0
@@ -57,6 +57,8 @@ std::string get_pid_exe(pid_t pid) {
80cbd0
   res = readlink(exe_link.c_str(), exe_path, sizeof(exe_path));
80cbd0
   if (res == -1)
80cbd0
     return "";
80cbd0
+  if (res >= sizeof(exe_path))
80cbd0
+    res = sizeof(exe_path) - 1;
80cbd0
   exe_path[res] = '\0';
80cbd0
   return std::string(exe_path);
80cbd0
 }
80cbd0
diff --git a/src/cc/frontends/b/parser.cc b/src/cc/frontends/b/parser.cc
80cbd0
index 9e61346..8a5e149 100644
80cbd0
--- a/src/cc/frontends/b/parser.cc
80cbd0
+++ b/src/cc/frontends/b/parser.cc
80cbd0
@@ -199,8 +199,10 @@ StmtNode * Parser::func_add(vector<int> *types, Scopes::StateScope *scope,
80cbd0
   auto cur_scope = scopes_->current_var();
80cbd0
   scopes_->set_current(scope);
80cbd0
   for (auto it = formals->begin(); it != formals->end(); ++it)
80cbd0
-    if (!variable_add(nullptr, it->get()))
80cbd0
+    if (!variable_add(nullptr, it->get())) {
80cbd0
+      delete decl;
80cbd0
       return nullptr;
80cbd0
+    }
80cbd0
   scopes_->set_current(cur_scope);
80cbd0
   decl->scope_ = scope;
80cbd0
   scopes_->top_func()->add(id->name_, decl);
80cbd0
diff --git a/src/cc/libbpf.c b/src/cc/libbpf.c
80cbd0
index c23030e..acfbc5e 100644
80cbd0
--- a/src/cc/libbpf.c
80cbd0
+++ b/src/cc/libbpf.c
80cbd0
@@ -925,6 +925,7 @@ static void exit_mount_ns(int fd) {
80cbd0
 
80cbd0
   if (setns(fd, CLONE_NEWNS))
80cbd0
     perror("setns");
80cbd0
+  close(fd);
80cbd0
 }
80cbd0
 
80cbd0
 int bpf_attach_uprobe(int progfd, enum bpf_probe_attach_type attach_type,
80cbd0
diff --git a/src/cc/usdt/usdt.cc b/src/cc/usdt/usdt.cc
80cbd0
index 2992593..2010520 100644
80cbd0
--- a/src/cc/usdt/usdt.cc
80cbd0
+++ b/src/cc/usdt/usdt.cc
80cbd0
@@ -478,8 +478,9 @@ const char *bcc_usdt_get_probe_argctype(
80cbd0
   void *ctx, const char* probe_name, const int arg_index
80cbd0
 ) {
80cbd0
   USDT::Probe *p = static_cast<USDT::Context *>(ctx)->get(probe_name);
80cbd0
-  std::string res = p ? p->get_arg_ctype(arg_index) : "";
80cbd0
-  return res.c_str();
80cbd0
+  if (p)
80cbd0
+    return p->get_arg_ctype(arg_index).c_str();
80cbd0
+  return "";
80cbd0
 }
80cbd0
 
80cbd0
 void bcc_usdt_foreach(void *usdt, bcc_usdt_cb callback) {
80cbd0
-- 
80cbd0
2.17.1
80cbd0