c2dfb7
From 73b87f8c73af714a32e7b56b217cd4e4f46a5ea7 Mon Sep 17 00:00:00 2001
c2dfb7
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
c2dfb7
Date: Wed, 8 May 2019 14:39:57 +0200
c2dfb7
Subject: [PATCH] sd-event: add sd_event_source_disable_unref() helper
c2dfb7
c2dfb7
(cherry picked from commit afd15bbb4b6414b9356799c63029e36642dae8e4)
c2dfb7
Related: CVE-2020-1712
c2dfb7
---
c2dfb7
 man/rules/meson.build              |  4 +++-
c2dfb7
 man/sd_event_source_unref.xml      | 30 +++++++++++++++++++-----------
c2dfb7
 src/libsystemd/libsystemd.sym      |  1 +
c2dfb7
 src/libsystemd/sd-event/sd-event.c |  6 ++++++
c2dfb7
 src/systemd/sd-event.h             |  1 +
c2dfb7
 5 files changed, 30 insertions(+), 12 deletions(-)
c2dfb7
c2dfb7
diff --git a/man/rules/meson.build b/man/rules/meson.build
c2dfb7
index 989d11c9b9..7ae94ea265 100644
c2dfb7
--- a/man/rules/meson.build
c2dfb7
+++ b/man/rules/meson.build
c2dfb7
@@ -340,7 +340,9 @@ manpages = [
c2dfb7
  ['sd_event_source_set_userdata', '3', ['sd_event_source_get_userdata'], ''],
c2dfb7
  ['sd_event_source_unref',
c2dfb7
   '3',
c2dfb7
-  ['sd_event_source_ref', 'sd_event_source_unrefp'],
c2dfb7
+  ['sd_event_source_disable_unref',
c2dfb7
+   'sd_event_source_ref',
c2dfb7
+   'sd_event_source_unrefp'],
c2dfb7
   ''],
c2dfb7
  ['sd_event_wait',
c2dfb7
   '3',
c2dfb7
diff --git a/man/sd_event_source_unref.xml b/man/sd_event_source_unref.xml
c2dfb7
index d1b83c57aa..af8fed33f2 100644
c2dfb7
--- a/man/sd_event_source_unref.xml
c2dfb7
+++ b/man/sd_event_source_unref.xml
c2dfb7
@@ -22,6 +22,7 @@
c2dfb7
     <refname>sd_event_source_unref</refname>
c2dfb7
     <refname>sd_event_source_unrefp</refname>
c2dfb7
     <refname>sd_event_source_ref</refname>
c2dfb7
+    <refname>sd_event_source_disable_unref</refname>
c2dfb7
 
c2dfb7
     <refpurpose>Increase or decrease event source reference counters</refpurpose>
c2dfb7
   </refnamediv>
c2dfb7
@@ -45,6 +46,10 @@
c2dfb7
         <paramdef>sd_event_source *<parameter>source</parameter></paramdef>
c2dfb7
       </funcprototype>
c2dfb7
 
c2dfb7
+      <funcprototype>
c2dfb7
+        <funcdef>sd_event_source* <function>sd_event_source_disable_unref</function></funcdef>
c2dfb7
+        <paramdef>sd_event_source *<parameter>source</parameter></paramdef>
c2dfb7
+      </funcprototype>
c2dfb7
     </funcsynopsis>
c2dfb7
   </refsynopsisdiv>
c2dfb7
 
c2dfb7
@@ -80,23 +85,26 @@
c2dfb7
     the passed event source object is
c2dfb7
     <constant>NULL</constant>.</para>
c2dfb7
 
c2dfb7
-    <para>Note that event source objects stay alive and may be
c2dfb7
-    dispatched as long as they have a reference counter greater than
c2dfb7
-    zero. In order to drop a reference of an event source and make
c2dfb7
-    sure the associated event source handler function is not called
c2dfb7
-    anymore it is recommended to combine a call of
c2dfb7
+    <para>Note that event source objects stay alive and may be dispatched as long as they have a reference
c2dfb7
+    counter greater than zero. In order to drop a reference of an event source and make sure the associated
c2dfb7
+    event source handler function is not called anymore it is recommended to combine a call of
c2dfb7
     <function>sd_event_source_unref()</function> with a prior call to
c2dfb7
-    <function>sd_event_source_set_enabled()</function> with
c2dfb7
-    <constant>SD_EVENT_OFF</constant>.</para>
c2dfb7
+    <function>sd_event_source_set_enabled()</function> with <constant>SD_EVENT_OFF</constant> or call
c2dfb7
+    <function>sd_event_source_disable_unref()</function>, see below.</para>
c2dfb7
+
c2dfb7
+    <para><function>sd_event_source_disable_unref()</function> combines a call to
c2dfb7
+    <function>sd_event_source_set_enabled()</function> with <constant>SD_EVENT_OFF</constant> with
c2dfb7
+    <function>sd_event_source_unref()</function>. This ensures that the source is disabled before the local
c2dfb7
+    reference to it is lost. The <parameter>source</parameter> parameter is allowed to be
c2dfb7
+    <constant>NULL</constant>.</para>
c2dfb7
   </refsect1>
c2dfb7
 
c2dfb7
   <refsect1>
c2dfb7
     <title>Return Value</title>
c2dfb7
 
c2dfb7
-    <para><function>sd_event_source_unref()</function> always returns
c2dfb7
-    <constant>NULL</constant>.
c2dfb7
-    <function>sd_event_source_ref()</function> always returns the
c2dfb7
-    event source object passed in.</para>
c2dfb7
+    <para><function>sd_event_source_unref()</function> and
c2dfb7
+    <function>sd_event_source_disable_unref()</function> always return <constant>NULL</constant>.
c2dfb7
+    <function>sd_event_source_ref()</function> always returns the event source object passed in.</para>
c2dfb7
   </refsect1>
c2dfb7
 
c2dfb7
   <xi:include href="libsystemd-pkgconfig.xml" />
c2dfb7
diff --git a/src/libsystemd/libsystemd.sym b/src/libsystemd/libsystemd.sym
c2dfb7
index e9972593a6..778e88a16c 100644
c2dfb7
--- a/src/libsystemd/libsystemd.sym
c2dfb7
+++ b/src/libsystemd/libsystemd.sym
c2dfb7
@@ -570,4 +570,5 @@ global:
c2dfb7
         sd_event_source_set_destroy_callback;
c2dfb7
         sd_event_source_get_destroy_callback;
c2dfb7
         sd_bus_enqueue_for_read;
c2dfb7
+        sd_event_source_disable_unref;
c2dfb7
 } LIBSYSTEMD_238;
c2dfb7
diff --git a/src/libsystemd/sd-event/sd-event.c b/src/libsystemd/sd-event/sd-event.c
c2dfb7
index d53b9a7026..0d3bf5cbb6 100644
c2dfb7
--- a/src/libsystemd/sd-event/sd-event.c
c2dfb7
+++ b/src/libsystemd/sd-event/sd-event.c
c2dfb7
@@ -580,6 +580,12 @@ _public_ sd_event* sd_event_unref(sd_event *e) {
c2dfb7
         return NULL;
c2dfb7
 }
c2dfb7
 
c2dfb7
+_public_ sd_event_source* sd_event_source_disable_unref(sd_event_source *s) {
c2dfb7
+        if (s)
c2dfb7
+                (void) sd_event_source_set_enabled(s, SD_EVENT_OFF);
c2dfb7
+        return sd_event_source_unref(s);
c2dfb7
+}
c2dfb7
+
c2dfb7
 static bool event_pid_changed(sd_event *e) {
c2dfb7
         assert(e);
c2dfb7
 
c2dfb7
diff --git a/src/systemd/sd-event.h b/src/systemd/sd-event.h
c2dfb7
index 7fcae4ac49..9876be01c6 100644
c2dfb7
--- a/src/systemd/sd-event.h
c2dfb7
+++ b/src/systemd/sd-event.h
c2dfb7
@@ -113,6 +113,7 @@ int sd_event_get_iteration(sd_event *e, uint64_t *ret);
c2dfb7
 
c2dfb7
 sd_event_source* sd_event_source_ref(sd_event_source *s);
c2dfb7
 sd_event_source* sd_event_source_unref(sd_event_source *s);
c2dfb7
+sd_event_source* sd_event_source_disable_unref(sd_event_source *s);
c2dfb7
 
c2dfb7
 sd_event *sd_event_source_get_event(sd_event_source *s);
c2dfb7
 void* sd_event_source_get_userdata(sd_event_source *s);