Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(687)

Side by Side Diff: dbus/object_proxy.cc

Issue 11199007: Add sender verification of D-Bus signals. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: use the default Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « dbus/object_proxy.h ('k') | dbus/signal_sender_verification_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "dbus/bus.h" 5 #include "dbus/bus.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/metrics/histogram.h" 10 #include "base/metrics/histogram.h"
11 #include "base/string_piece.h" 11 #include "base/string_piece.h"
12 #include "base/stringprintf.h" 12 #include "base/stringprintf.h"
13 #include "base/threading/thread.h" 13 #include "base/threading/thread.h"
14 #include "base/threading/thread_restrictions.h" 14 #include "base/threading/thread_restrictions.h"
15 #include "dbus/message.h" 15 #include "dbus/message.h"
16 #include "dbus/object_path.h" 16 #include "dbus/object_path.h"
17 #include "dbus/object_proxy.h" 17 #include "dbus/object_proxy.h"
18 #include "dbus/scoped_dbus_error.h" 18 #include "dbus/scoped_dbus_error.h"
19 19
20 namespace { 20 namespace {
21 21
22 const char kErrorServiceUnknown[] = "org.freedesktop.DBus.Error.ServiceUnknown"; 22 const char kErrorServiceUnknown[] = "org.freedesktop.DBus.Error.ServiceUnknown";
23 23
24 // Used for success ratio histograms. 1 for success, 0 for failure. 24 // Used for success ratio histograms. 1 for success, 0 for failure.
25 const int kSuccessRatioHistogramMaxValue = 2; 25 const int kSuccessRatioHistogramMaxValue = 2;
26 26
27 // The path of D-Bus Object sending NameOwnerChanged signal.
28 const char kDbusSystemObjectPath[] = "/org/freedesktop/DBus";
29
27 // Gets the absolute signal name by concatenating the interface name and 30 // Gets the absolute signal name by concatenating the interface name and
28 // the signal name. Used for building keys for method_table_ in 31 // the signal name. Used for building keys for method_table_ in
29 // ObjectProxy. 32 // ObjectProxy.
30 std::string GetAbsoluteSignalName( 33 std::string GetAbsoluteSignalName(
31 const std::string& interface_name, 34 const std::string& interface_name,
32 const std::string& signal_name) { 35 const std::string& signal_name) {
33 return interface_name + "." + signal_name; 36 return interface_name + "." + signal_name;
34 } 37 }
35 38
36 // An empty function used for ObjectProxy::EmptyResponseCallback(). 39 // An empty function used for ObjectProxy::EmptyResponseCallback().
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after
355 filter_added_ = true; 358 filter_added_ = true;
356 } else { 359 } else {
357 LOG(ERROR) << "Failed to add filter function"; 360 LOG(ERROR) << "Failed to add filter function";
358 } 361 }
359 } 362 }
360 // Add a match rule so the signal goes through HandleMessage(). 363 // Add a match rule so the signal goes through HandleMessage().
361 const std::string match_rule = 364 const std::string match_rule =
362 base::StringPrintf("type='signal', interface='%s', path='%s'", 365 base::StringPrintf("type='signal', interface='%s', path='%s'",
363 interface_name.c_str(), 366 interface_name.c_str(),
364 object_path_.value().c_str()); 367 object_path_.value().c_str());
365 368 // Add a match_rule listening NameOwnerChanged for the well-known name
366 // Add the match rule if we don't have it. 369 // |service_name_|.
367 if (match_rules_.find(match_rule) == match_rules_.end()) { 370 const std::string name_owner_changed_match_rule =
368 ScopedDBusError error; 371 base::StringPrintf(
369 bus_->AddMatch(match_rule, error.get());; 372 "type='signal',interface='org.freedesktop.DBus',"
370 if (error.is_set()) { 373 "member='NameOwnerChanged',path='/org/freedesktop/DBus',"
371 LOG(ERROR) << "Failed to add match rule: " << match_rule; 374 "sender='org.freedesktop.DBus',arg0='%s'",
372 } else { 375 service_name_.c_str());
373 // Store the match rule, so that we can remove this in Detach(). 376 if (AddMatchRuleWithCallback(match_rule,
374 match_rules_.insert(match_rule); 377 absolute_signal_name,
375 // Add the signal callback to the method table. 378 signal_callback) &&
376 method_table_[absolute_signal_name] = signal_callback; 379 AddMatchRuleWithoutCallback(name_owner_changed_match_rule,
377 success = true; 380 "org.freedesktop.DBus.NameOwnerChanged")) {
378 }
379 } else {
380 // We already have the match rule.
381 method_table_[absolute_signal_name] = signal_callback;
382 success = true; 381 success = true;
383 } 382 }
383
384 // Try getting the current name owner. It's not guaranteed that we can get
385 // the name owner at this moment, as the service may not yet be started. If
386 // that's the case, we'll get the name owner via NameOwnerChanged signal,
387 // as soon as the service is started.
388 UpdateNameOwnerAndBlock();
384 } 389 }
385 390
386 // Run on_connected_callback in the origin thread. 391 // Run on_connected_callback in the origin thread.
387 bus_->PostTaskToOriginThread( 392 bus_->PostTaskToOriginThread(
388 FROM_HERE, 393 FROM_HERE,
389 base::Bind(&ObjectProxy::OnConnected, 394 base::Bind(&ObjectProxy::OnConnected,
390 this, 395 this,
391 on_connected_callback, 396 on_connected_callback,
392 interface_name, 397 interface_name,
393 signal_name, 398 signal_name,
394 success)); 399 success));
395 } 400 }
396 401
397 void ObjectProxy::OnConnected(OnConnectedCallback on_connected_callback, 402 void ObjectProxy::OnConnected(OnConnectedCallback on_connected_callback,
398 const std::string& interface_name, 403 const std::string& interface_name,
399 const std::string& signal_name, 404 const std::string& signal_name,
400 bool success) { 405 bool success) {
401 bus_->AssertOnOriginThread(); 406 bus_->AssertOnOriginThread();
402 407
403 on_connected_callback.Run(interface_name, signal_name, success); 408 on_connected_callback.Run(interface_name, signal_name, success);
404 } 409 }
405 410
406 DBusHandlerResult ObjectProxy::HandleMessage( 411 DBusHandlerResult ObjectProxy::HandleMessage(
407 DBusConnection* connection, 412 DBusConnection* connection,
408 DBusMessage* raw_message) { 413 DBusMessage* raw_message) {
409 bus_->AssertOnDBusThread(); 414 bus_->AssertOnDBusThread();
415
410 if (dbus_message_get_type(raw_message) != DBUS_MESSAGE_TYPE_SIGNAL) 416 if (dbus_message_get_type(raw_message) != DBUS_MESSAGE_TYPE_SIGNAL)
411 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; 417 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
412 418
413 // raw_message will be unrefed on exit of the function. Increment the 419 // raw_message will be unrefed on exit of the function. Increment the
414 // reference so we can use it in Signal. 420 // reference so we can use it in Signal.
415 dbus_message_ref(raw_message); 421 dbus_message_ref(raw_message);
416 scoped_ptr<Signal> signal( 422 scoped_ptr<Signal> signal(
417 Signal::FromRawMessage(raw_message)); 423 Signal::FromRawMessage(raw_message));
418 424
419 // Verify the signal comes from the object we're proxying for, this is 425 // Verify the signal comes from the object we're proxying for, this is
420 // our last chance to return DBUS_HANDLER_RESULT_NOT_YET_HANDLED and 426 // our last chance to return DBUS_HANDLER_RESULT_NOT_YET_HANDLED and
421 // allow other object proxies to handle instead. 427 // allow other object proxies to handle instead.
422 const dbus::ObjectPath path = signal->GetPath(); 428 const dbus::ObjectPath path = signal->GetPath();
423 if (path != object_path_) { 429 if (path != object_path_) {
430 if (path.value() == kDbusSystemObjectPath &&
431 signal->GetMember() == "NameOwnerChanged") {
432 // Handle NameOwnerChanged separately
433 return HandleNameOwnerChanged(signal.get());
434 }
424 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; 435 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
425 } 436 }
426 437
427 const std::string interface = signal->GetInterface(); 438 const std::string interface = signal->GetInterface();
428 const std::string member = signal->GetMember(); 439 const std::string member = signal->GetMember();
429 440
430 // Check if we know about the signal. 441 // Check if we know about the signal.
431 const std::string absolute_signal_name = GetAbsoluteSignalName( 442 const std::string absolute_signal_name = GetAbsoluteSignalName(
432 interface, member); 443 interface, member);
433 MethodTable::const_iterator iter = method_table_.find(absolute_signal_name); 444 MethodTable::const_iterator iter = method_table_.find(absolute_signal_name);
434 if (iter == method_table_.end()) { 445 if (iter == method_table_.end()) {
435 // Don't know about the signal. 446 // Don't know about the signal.
436 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; 447 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
437 } 448 }
438 VLOG(1) << "Signal received: " << signal->ToString(); 449 VLOG(1) << "Signal received: " << signal->ToString();
439 450
451 std::string sender = signal->GetSender();
452 if (service_name_owner_ != sender) {
453 LOG(ERROR) << "Rejecting a message from a wrong sender.";
454 UMA_HISTOGRAM_COUNTS("DBus.RejectedSignalCount", 1);
455 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
456 }
457
440 const base::TimeTicks start_time = base::TimeTicks::Now(); 458 const base::TimeTicks start_time = base::TimeTicks::Now();
441 if (bus_->HasDBusThread()) { 459 if (bus_->HasDBusThread()) {
442 // Post a task to run the method in the origin thread. 460 // Post a task to run the method in the origin thread.
443 // Transfer the ownership of |signal| to RunMethod(). 461 // Transfer the ownership of |signal| to RunMethod().
444 // |released_signal| will be deleted in RunMethod(). 462 // |released_signal| will be deleted in RunMethod().
445 Signal* released_signal = signal.release(); 463 Signal* released_signal = signal.release();
446 bus_->PostTaskToOriginThread(FROM_HERE, 464 bus_->PostTaskToOriginThread(FROM_HERE,
447 base::Bind(&ObjectProxy::RunMethod, 465 base::Bind(&ObjectProxy::RunMethod,
448 this, 466 this,
449 start_time, 467 start_time,
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
508 std::string error_message; 526 std::string error_message;
509 reader.PopString(&error_message); 527 reader.PopString(&error_message);
510 LogMethodCallFailure(interface_name, 528 LogMethodCallFailure(interface_name,
511 method_name, 529 method_name,
512 error_response->GetErrorName(), 530 error_response->GetErrorName(),
513 error_message); 531 error_message);
514 } 532 }
515 response_callback.Run(NULL); 533 response_callback.Run(NULL);
516 } 534 }
517 535
536 bool ObjectProxy::AddMatchRuleWithCallback(
537 const std::string& match_rule,
538 const std::string& absolute_signal_name,
539 SignalCallback signal_callback) {
540 DCHECK(!match_rule.empty());
541 DCHECK(!absolute_signal_name.empty());
542 bus_->AssertOnDBusThread();
543
544 if (match_rules_.find(match_rule) == match_rules_.end()) {
545 ScopedDBusError error;
546 bus_->AddMatch(match_rule, error.get());
547 if (error.is_set()) {
548 LOG(ERROR) << "Failed to add match rule \"" << match_rule << "\". Got " <<
549 error.name() << ": " << error.message();
550 return false;
551 } else {
552 // Store the match rule, so that we can remove this in Detach().
553 match_rules_.insert(match_rule);
554 // Add the signal callback to the method table.
555 method_table_[absolute_signal_name] = signal_callback;
556 return true;
557 }
558 } else {
559 // We already have the match rule.
560 method_table_[absolute_signal_name] = signal_callback;
561 return true;
562 }
563 }
564
565 bool ObjectProxy::AddMatchRuleWithoutCallback(
566 const std::string& match_rule,
567 const std::string& absolute_signal_name) {
568 DCHECK(!match_rule.empty());
569 DCHECK(!absolute_signal_name.empty());
570 bus_->AssertOnDBusThread();
571
572 if (match_rules_.find(match_rule) != match_rules_.end())
573 return true;
574
575 ScopedDBusError error;
576 bus_->AddMatch(match_rule, error.get());
577 if (error.is_set()) {
578 LOG(ERROR) << "Failed to add match rule \"" << match_rule << "\". Got " <<
579 error.name() << ": " << error.message();
580 return false;
581 }
582 // Store the match rule, so that we can remove this in Detach().
583 match_rules_.insert(match_rule);
584 return true;
585 }
586
587 void ObjectProxy::UpdateNameOwnerAndBlock() {
588 bus_->AssertOnDBusThread();
589
590 MethodCall get_name_owner_call("org.freedesktop.DBus", "GetNameOwner");
591 MessageWriter writer(&get_name_owner_call);
592 writer.AppendString(service_name_);
593 VLOG(1) << "Method call: " << get_name_owner_call.ToString();
594
595 const dbus::ObjectPath obj_path("/org/freedesktop/DBus");
596 ScopedDBusError error;
597 if (!get_name_owner_call.SetDestination("org.freedesktop.DBus") ||
598 !get_name_owner_call.SetPath(obj_path)) {
599 LOG(ERROR) << "Failed to get name owner.";
600 return;
601 }
602
603 DBusMessage* response_message = bus_->SendWithReplyAndBlock(
604 get_name_owner_call.raw_message(),
605 TIMEOUT_USE_DEFAULT,
606 error.get());
607 if (!response_message) {
608 LOG(ERROR) << "Failed to get name owner. Got " << error.name() << ": " <<
609 error.message();
610 return;
611 }
612 scoped_ptr<Response> response(Response::FromRawMessage(response_message));
613 MessageReader reader(response.get());
614
615 std::string new_service_name_owner;
616 if (reader.PopString(&new_service_name_owner))
617 service_name_owner_ = new_service_name_owner;
618 else
619 service_name_owner_.clear();
620 }
621
622 DBusHandlerResult ObjectProxy::HandleNameOwnerChanged(Signal* signal) {
623 DCHECK(signal);
624 bus_->AssertOnDBusThread();
625
626 // Confirm the validity of the NameOwnerChanged signal.
627 if (signal->GetMember() == "NameOwnerChanged" &&
628 signal->GetInterface() == "org.freedesktop.DBus" &&
629 signal->GetSender() == "org.freedesktop.DBus") {
630 MessageReader reader(signal);
631 std::string name, old_owner, new_owner;
632 if (reader.PopString(&name) &&
633 reader.PopString(&old_owner) &&
634 reader.PopString(&new_owner) &&
635 name == service_name_) {
636 service_name_owner_ = new_owner;
637 return DBUS_HANDLER_RESULT_HANDLED;
638 }
639 }
640
641 // Untrusted or uninteresting signal
642 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
643 }
644
518 } // namespace dbus 645 } // namespace dbus
OLDNEW
« no previous file with comments | « dbus/object_proxy.h ('k') | dbus/signal_sender_verification_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698