• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2014 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include <sysexits.h>
18 
19 #include <memory>
20 #include <string>
21 
22 #include <base/command_line.h>
23 #include <brillo/daemons/dbus_daemon.h>
24 #include <brillo/dbus/async_event_sequencer.h>
25 #include <brillo/minijail/minijail.h>
26 #include <brillo/syslog_logging.h>
27 #include <brillo/userdb_utils.h>
28 
29 #include "attestation/common/dbus_interface.h"
30 #include "attestation/server/attestation_service.h"
31 #include "attestation/server/dbus_service.h"
32 
33 #include <chromeos/libminijail.h>
34 
35 namespace {
36 
37 const uid_t kRootUID = 0;
38 const char kAttestationUser[] = "attestation";
39 const char kAttestationGroup[] = "attestation";
40 const char kAttestationSeccompPath[] =
41     "/usr/share/policy/attestationd-seccomp.policy";
42 
InitMinijailSandbox()43 void InitMinijailSandbox() {
44   uid_t attestation_uid;
45   gid_t attestation_gid;
46   CHECK(brillo::userdb::GetUserInfo(kAttestationUser, &attestation_uid,
47                                     &attestation_gid))
48       << "Error getting attestation uid and gid.";
49   CHECK_EQ(getuid(), kRootUID) << "AttestationDaemon not initialized as root.";
50   brillo::Minijail* minijail = brillo::Minijail::GetInstance();
51   struct minijail* jail = minijail->New();
52 
53   minijail->DropRoot(jail, kAttestationUser, kAttestationGroup);
54   minijail_inherit_usergroups(jail);
55   minijail->UseSeccompFilter(jail, kAttestationSeccompPath);
56   minijail->Enter(jail);
57   minijail->Destroy(jail);
58   CHECK_EQ(getuid(), attestation_uid)
59       << "AttestationDaemon was not able to drop to attestation user.";
60   CHECK_EQ(getgid(), attestation_gid)
61       << "AttestationDaemon was not able to drop to attestation group.";
62 }
63 
64 }  // namespace
65 
66 using brillo::dbus_utils::AsyncEventSequencer;
67 
68 class AttestationDaemon : public brillo::DBusServiceDaemon {
69  public:
AttestationDaemon()70   AttestationDaemon()
71       : brillo::DBusServiceDaemon(attestation::kAttestationServiceName) {
72     attestation_service_.reset(new attestation::AttestationService);
73     // Move initialize call down to OnInit
74     CHECK(attestation_service_->Initialize());
75   }
76 
77  protected:
OnInit()78   int OnInit() override {
79     int result = brillo::DBusServiceDaemon::OnInit();
80     if (result != EX_OK) {
81       LOG(ERROR) << "Error starting attestation dbus daemon.";
82       return result;
83     }
84     return EX_OK;
85   }
86 
RegisterDBusObjectsAsync(AsyncEventSequencer * sequencer)87   void RegisterDBusObjectsAsync(AsyncEventSequencer* sequencer) override {
88     dbus_service_.reset(
89         new attestation::DBusService(bus_, attestation_service_.get()));
90     dbus_service_->Register(sequencer->GetHandler("Register() failed.", true));
91   }
92 
93  private:
94   std::unique_ptr<attestation::AttestationInterface> attestation_service_;
95   std::unique_ptr<attestation::DBusService> dbus_service_;
96 
97   DISALLOW_COPY_AND_ASSIGN(AttestationDaemon);
98 };
99 
main(int argc,char * argv[])100 int main(int argc, char* argv[]) {
101   base::CommandLine::Init(argc, argv);
102   base::CommandLine* cl = base::CommandLine::ForCurrentProcess();
103   int flags = brillo::kLogToSyslog;
104   if (cl->HasSwitch("log_to_stderr")) {
105     flags |= brillo::kLogToStderr;
106   }
107   brillo::InitLog(flags);
108   AttestationDaemon daemon;
109   LOG(INFO) << "Attestation Daemon Started.";
110   InitMinijailSandbox();
111   return daemon.Run();
112 }
113