public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Maninder Singh <maninder1.s@samsung.com>
To: libc-alpha@sourceware.org, triegel@redhat.com, szabolcs.nagy@arm.com
Cc: pankaj.m@samsung.com, ajeet.y@samsung.com,
	a.sahrawat@samsung.com, lalit.mohan@samsung.com,
	akhilesh.k@samsung.com, hakbong5.lee@samsung.com,
	Maninder Singh <maninder1.s@samsung.com>,
	Vaneet Narang <v.narang@samsung.com>
Subject: [PATCH v3] dl-load: add memory barrier before updating the next
Date: Mon, 03 Apr 2017 11:30:00 -0000	[thread overview]
Message-ID: <1491218925-19517-1-git-send-email-maninder1.s@samsung.com> (raw)
In-Reply-To: <CGME20170403113000epcas5p37d569cf71eda2d413cfab5e7f7e3591d@epcas5p3.samsung.com>

[BZ #21349]: race condition between dl_open and rtld lazy symbol resolve.

This patch adds C11 memory barrier before updating the liblist next.

Issue Fix: race condition between add_name_to_object  & _dl_name_match_p.
One threads calling dlopen which further calls add_name_to_object &
other thread trying to resolve RTLD_LAZY symbols through
_dl_runtime_resolve which further calls.

_dl_name_match_p checks if libname->next is valid, then it assumes
libname->next->name to be valid. Also add_name_to_object initialized
name first and then sets valid next pointer.

This patch avoids any reorder of instruction when next is set before
name to avoid any race.

Signed-off-by: Vaneet Narang <v.narang@samsung.com>
Signed-off-by: Maninder Singh <maninder1.s@samsung.com>
---
v1 -> v2: use C11 atomics rather than direct memory barriers.
v2 -> v3: use comments for barriers and enter Bugzilla ID.

 elf/dl-load.c |    5 ++++-
 elf/dl-misc.c |    5 ++++-
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/elf/dl-load.c b/elf/dl-load.c
index a5318f9..03c6afb 100644
--- a/elf/dl-load.c
+++ b/elf/dl-load.c
@@ -418,7 +418,10 @@ add_name_to_object (struct link_map *l, const char *name)
   newname->name = memcpy (newname + 1, name, name_len);
   newname->next = NULL;
   newname->dont_free = 0;
-  lastp->next = newname;
+  /* We need release memory order here because we need to synchronize
+     with other thread doing _dl_runtime_resolve which calls _dl_name_match_p
+     to traverse all names added to libname_list*/
+  atomic_store_release (&(lastp->next), newname);
 }
 
 /* Standard search directories.  */
diff --git a/elf/dl-misc.c b/elf/dl-misc.c
index 1e9a6ee..a26d6f6 100644
--- a/elf/dl-misc.c
+++ b/elf/dl-misc.c
@@ -295,7 +295,10 @@ _dl_name_match_p (const char *name, const struct link_map *map)
     if (strcmp (name, runp->name) == 0)
       return 1;
     else
-      runp = runp->next;
+      /* We need to acquire memory order here because we need to synchronize
+         with other thread calling dlopen and adding new name to libname_list
+         through add_name_to_object */
+      runp = atomic_load_acquire(&(runp->next));
 
   return 0;
 }
-- 
1.7.1

       reply	other threads:[~2017-04-03 11:30 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20170403113000epcas5p37d569cf71eda2d413cfab5e7f7e3591d@epcas5p3.samsung.com>
2017-04-03 11:30 ` Maninder Singh [this message]
2017-04-03 11:45   ` Andreas Schwab
2017-04-03 13:25   ` Torvald Riegel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1491218925-19517-1-git-send-email-maninder1.s@samsung.com \
    --to=maninder1.s@samsung.com \
    --cc=a.sahrawat@samsung.com \
    --cc=ajeet.y@samsung.com \
    --cc=akhilesh.k@samsung.com \
    --cc=hakbong5.lee@samsung.com \
    --cc=lalit.mohan@samsung.com \
    --cc=libc-alpha@sourceware.org \
    --cc=pankaj.m@samsung.com \
    --cc=szabolcs.nagy@arm.com \
    --cc=triegel@redhat.com \
    --cc=v.narang@samsung.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).