<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet title="XSL formatting" type="text/xsl" href="http://scox.info/feed/rss2/xslt" ?><rss version="2.0"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
  <title>scox.info - Random Thoughts - HAL</title>
  <link>http://scox.info/</link>
  <description></description>
  <language>en</language>
  <pubDate>Tue, 06 Jan 2009 23:47:29 +0100</pubDate>
  <copyright></copyright>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Dotclear</generator>
  
    
  <item>
    <title>Using D-Bus from setuid applications</title>
    <link>http://scox.info/post/2007/8/25/using-dbus-from-setuid-applications</link>
    <guid isPermaLink="false">urn:md5:7096cfdf61cec980749d348fba4c8ca1</guid>
    <pubDate>Sat, 25 Aug 2007 15:00:00 +0000</pubDate>
    <dc:creator>scox</dc:creator>
        <category>code</category>
        <category>DBUS</category><category>HAL</category>    
    <description>    &lt;p&gt;When trying to connect to the system bus from a setuid application, D-Bus throws back the following error:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken.&lt;/p&gt;
&lt;/blockquote&gt;&lt;p&gt;After some debugging, I found that the problem is a bug in the D-Bus EXTERNAL authentication method: the library sends the real UID while the daemon checks the effective UID, which of course doesn't work at all in setuid applications.&lt;/p&gt;
&lt;p&gt;I filed a &lt;a href=&quot;https://bugs.freedesktop.org/show_bug.cgi?id=11876&quot;&gt;bug report&lt;/a&gt; in the &lt;a href=&quot;https://bugs.freedesktop.org/show_bug.cgi?id=11876&quot;&gt;Freedesktop Bugzilla&lt;/a&gt; and provideda &lt;a href=&quot;https://bugs.freedesktop.org/attachment.cgi?id=11021&quot;&gt;patch&lt;/a&gt; which is yet to be merged.&lt;/p&gt;
&lt;p&gt;In the meantime, if you need to use D-Bus in a setuid application, the following code might help:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;DBusConnection *my_dbus_bus_get(DBusBusType type, DBusError *error)&lt;br /&gt;{&lt;br /&gt;        DBusConnection  *bus = NULL;&lt;br /&gt;&lt;br /&gt;        if (!(bus = dbus_bus_get(type, error)))&lt;br /&gt;        {&lt;br /&gt;                /* The connection to the BUS failed, we now check&lt;br /&gt;                 * if we are running as setuid. */&lt;br /&gt;                uid_t                   ruid;&lt;br /&gt;                uid_t                   euid;&lt;br /&gt;&lt;br /&gt;                if (!(euid = geteuid()) &amp;amp;&amp;amp; (ruid = getuid()))&lt;br /&gt;                {&lt;br /&gt;                        /* In that case, we temporary change our&lt;br /&gt;                         * real uid to the effective uid and try again */&lt;br /&gt;                        dbus_error_free(error);&lt;br /&gt;                        setreuid(euid, euid);&lt;br /&gt;                        bus = dbus_bus_get(type, error);&lt;br /&gt;                        setreuid(ruid, euid);&lt;br /&gt;                }&lt;br /&gt;        }&lt;br /&gt;        return bus;&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This workaround is a function you'll have to call instead of the regular &lt;em&gt;dbus_bus_get()&lt;/em&gt;. In case the connection fails and it's running on a setuid application, it will change the real UID to match the effective UID so the authentication process will succeed, make a connection to D-Bus and restore everything back.&lt;/p&gt;</description>
    
    
    
          <comments>http://scox.info/post/2007/8/25/using-dbus-from-setuid-applications#comment-form</comments>
      <wfw:comment>http://scox.info/post/2007/8/25/using-dbus-from-setuid-applications#comment-form</wfw:comment>
      <wfw:commentRss>http://scox.info/feed/rss2/comments/4</wfw:commentRss>
      </item>
    
</channel>
</rss>