#!/usr/bin/env python import sys import argparse import dbus import time def get_cmdline(pid): cmdline = '' if pid > 0: try: procpath = '/proc/' + str(pid) + '/cmdline' with open(procpath, 'r') as f: cmdline = " ".join(f.readline().split('\0')) except: pass return cmdline parser = argparse.ArgumentParser(description='Testing D-Bus match rules') parser.add_argument('--session', help='session bus', action="store_true") parser.add_argument('--system', help='system bus', action="store_true") args = parser.parse_args() if args.system: bus = dbus.SystemBus() else: bus = dbus.SessionBus() remote_object = bus.get_object("org.freedesktop.DBus", "/org/freedesktop/DBus") bus_iface = dbus.Interface(remote_object, "org.freedesktop.DBus") stats_iface = dbus.Interface(remote_object, "org.freedesktop.DBus.Debug.Stats") try: match_rules = stats_iface.GetConnectionMatchRules() except: print("GetConnectionMatchRules failed: did you enable the Stats interface?") print(" org.freedesktop.DBus.Debug.Stats") sys.exit(1) names = bus_iface.ListNames() unique_names = [ a for a in names if a.startswith(":") ] pids = dict((name, bus_iface.GetConnectionUnixProcessID(name)) for name in unique_names) cmds = dict((name, get_cmdline(pids[name])) for name in unique_names) well_known_names = [ a for a in names if a not in unique_names ] owners = dict((wkn, bus_iface.GetNameOwner(wkn)) for wkn in well_known_names) rules = dict((k_rules, dict({'wkn': [k for k, v in owners.items() if v == k_rules], 'pid': pids[k_rules], 'cmd': cmds[k_rules] or "", 'rules': v_rules})) for k_rules, v_rules in match_rules.items()) ret = 0 for name in rules: bad_rules = 0 rules_without_signal_type = [] for rule in rules[name]['rules']: if "type='signal'" not in rule: bad_rules = bad_rules + 1 rules_without_signal_type.append(rule) rules_without_sender = [] for rule in rules[name]['rules']: if "sender=" not in rule: bad_rules = bad_rules + 1 rules_without_sender.append(rule) local_rules = [] for rule in rules[name]['rules']: if "interface='org.freedesktop.DBus.Local'" in rule: bad_rules = bad_rules + 1 local_rules.append(rule) name_owner_changed_without_arg0 = [] for rule in rules[name]['rules']: if "member='NameOwnerChanged'" in rule and "arg0=" not in rule: bad_rules = bad_rules + 1 name_owner_changed_without_arg0.append(rule) if bad_rules > 0: print("Connection %s with pid %d '%s' (%s) has the following suspicious rules:" % (name, rules[name]['pid'], rules[name]['cmd'], ' '.join(rules[name]['wkn']))) if len(local_rules) > 0: print(" - Match rules on org.freedesktop.DBus.Local:") for rule in local_rules: print(" - %s" % (rule)) if len(rules_without_signal_type) > 0: print(" - Match rules without signal type:") for rule in rules_without_signal_type: print(" - %s" % (rule)) if len(rules_without_sender) > 0: print(" - Match rules without sender:") for rule in rules_without_sender: print(" - %s" % (rule)) if len(name_owner_changed_without_arg0) > 0: print(" - Match rules about NameOwnerChanged without arg0:") for rule in name_owner_changed_without_arg0: print(" - %s" % (rule)) print("") sys.exit(ret)