Blame SOURCES/0237-sd-event-add-sd_event_source_disable_unref-helper.patch

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