Change krate min/max UID
[platform/core/security/krate.git] / module / krate.cpp
1 /*
2  *  Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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 <dlfcn.h>
18 #include <syslog.h>
19 #include <unistd.h>
20 #include <security/pam_ext.h>
21 #include <security/pam_modules.h>
22
23 #include <string>
24 #include <vector>
25 #include <iostream>
26
27 #include "krate-builder.h"
28
29 #include <klay/cgroup.h>
30 #include <klay/namespace.h>
31 #include <klay/exception.h>
32 #include <klay/filesystem.h>
33 #include <klay/xml/parser.h>
34 #include <klay/xml/document.h>
35
36 #define CGROUP_SUBSYSTEM "krate"
37
38 #define KRATE_UID_MIN 6000
39 #define KRATE_UID_MAX 6999
40
41 #define LAZYMOUNT_EXTERN extern
42 #define LAZYMOUNT_LIB "/usr/lib/liblazymount.so.0"
43
44 std::string buildKrateManifestPath(const std::string& name)
45 {
46         return "/home/" + name + "/.config/krate/krate.xml";
47 }
48
49 std::string getKrateName(pam_handle_t* handle)
50 {
51         const void* retItem;
52         int error = ::pam_get_item(handle, PAM_USER, &retItem);
53         if (error != PAM_SUCCESS) {
54                 throw runtime::Exception("Failed to get user");
55         }
56
57         return static_cast<const char*>(retItem);
58 }
59
60 void enterKrate(const std::string& name)
61 {
62         std::string path = CGROUP_SUBSYSTEM "/" + name;
63         pid_t pid = 0;
64
65         if (runtime::Cgroup::exist(CGROUP_SUBSYSTEM, path)) {
66                 auto pids = runtime::Cgroup::getProcessList(CGROUP_SUBSYSTEM, path);
67                 if (pids.size() > 0) {
68                         pid  = pids[0];
69                 }
70         } else {
71                 runtime::Cgroup::create(CGROUP_SUBSYSTEM, path);
72         }
73
74         if (pid == 0) {
75                 runtime::Cgroup::addProcess(CGROUP_SUBSYSTEM, path, ::getpid());
76                 runtime::Namespace::unshare(CLONE_NEWNS | CLONE_NEWIPC);
77         } else {
78                 runtime::Namespace::attach(pid);
79         }
80 }
81
82 static int wait_condition(void)
83 {
84     int r;
85     void *h;
86
87     int (*wait_mount_user)(void);
88
89     r = access(LAZYMOUNT_LIB, F_OK);
90     if (r < 0){
91         fprintf(stderr, "cannot find lazymount module - No support lazymount\n");
92         return 0;
93     }
94
95     h = dlopen(LAZYMOUNT_LIB, RTLD_LAZY);
96     if (!h) {
97         fprintf(stderr, "lazymount module dlopen error\n");
98         return -1;
99     }
100
101         do{
102         wait_mount_user = (int (*)())dlsym(h, "wait_mount_user");
103         if (!wait_mount_user) {
104             fprintf(stderr, "dlsym wait_mount_user error\n");
105             dlclose(h);
106             return -1;
107         }
108     } while (0);
109
110     r = wait_mount_user();
111     if (r < 0) {
112         fprintf(stderr, "wait_mout_user failed\n");
113         dlclose(h);
114         return r;
115     }
116
117     dlclose(h);
118     return 0;
119 }
120
121 extern "C" {
122 LAZYMOUNT_EXTERN  __attribute__((visibility("default")))
123 int container_preprocess(char* id) {
124         std::cout << "kraterize (UID " << id << ")..." << std::endl << std::flush;
125         try {
126                 runtime::User user(std::stoi(std::string(id)));
127
128                 enterKrate(user.getName());
129
130                 if (user.getUid() >= KRATE_UID_MIN && user.getUid() <= KRATE_UID_MAX ) {
131                         wait_condition();
132                 }
133         } catch (runtime::Exception& e) {
134                 std::cerr << "krate error : " << e.what() <<std::endl << std::flush;
135                 return -1;
136         }
137
138         std::cout << "krate preprocess completed!" << std::endl << std::flush;
139         return 0;
140 }
141
142 LAZYMOUNT_EXTERN  __attribute__((visibility("default")))
143 int container_postprocess(char* id) {
144         try {
145                 runtime::User user(std::stoi(std::string(id)));
146                 KrateBuilder builder(user, buildKrateManifestPath(user.getName()));
147                 builder.mountOwnFilesystem();
148         } catch (runtime::Exception& e) {
149                 std::cerr << "krate error : " << e.what() << std::endl << std::flush;
150                 return -1;
151         }
152         std::cout << "krate postprocess completed!" << std::endl << std::flush;
153         std::cout << "kraterized!" << std::endl << std::flush;
154         return 0;
155 }
156
157 PAM_EXTERN  __attribute__((visibility("default")))
158 int pam_sm_open_session(pam_handle_t* pamh, int flags, int argc, const char* argv[])
159 {
160         try {
161                 runtime::User user(getKrateName(pamh));
162
163                 enterKrate(user.getName());
164
165                 KrateBuilder builder(user, buildKrateManifestPath(user.getName()));
166                 builder.mountOwnFilesystem();
167         } catch (runtime::Exception& e) {
168                 ::pam_syslog(pamh, LOG_ERR, "%s", e.what());
169                 return PAM_SESSION_ERR;
170         }
171
172         return PAM_SUCCESS;
173 }
174
175 PAM_EXTERN  __attribute__((visibility("default")))
176 int pam_sm_close_session(pam_handle_t* pamh, int flags, int argc, const char* argv[])
177 {
178         try {
179                 std::string name = getKrateName(pamh);
180                 auto pids = runtime::Cgroup::getProcessList(CGROUP_SUBSYSTEM, name);
181                 if (pids.size() <= 1) {
182                         std::string path = CGROUP_SUBSYSTEM "/" + name;
183                         runtime::Cgroup::destroy(CGROUP_SUBSYSTEM, path);
184                 }
185         } catch (runtime::Exception& e) {
186                 ::pam_syslog(pamh, LOG_ERR, "%s", e.what());
187                 return PAM_SESSION_ERR;
188         }
189
190         return PAM_SUCCESS;
191 }
192
193 #ifdef PAM_MODULE_ENTRY
194 PAM_MODULE_ENTRY("pam_krate");
195 #endif
196
197 }