2395a2a79f46ab5aba713adb12549730a84ae051
[framework/web/webkit-efl.git] / Source / WebKit2 / Shared / tizen / ProcessSmackLabel.cpp
1 /*
2  * Copyright (C) 2013 Samsung Electronics. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #if ENABLE(TIZEN_PROCESS_PERMISSION_CONTROL)
27
28 #include <string>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <sys/smack.h>
34 #include <sys/capability.h>
35 #include <wtf/Assertions.h>
36
37 namespace WebKit {
38
39 bool changeProcessSmackLabel(const char* defaultExecutablePath, const char* currentExecutablePath)
40 {
41     ASSERT(defaultExecutablePath && currentExecutablePath);
42
43     // this case needs not to change smack label
44     if (!strcmp(defaultExecutablePath, currentExecutablePath))
45         return true;
46
47     // check if this process is launched as abnormal way
48     char* newLabel;
49     if (smack_lgetlabel(currentExecutablePath, &newLabel, SMACK_LABEL_EXEC) < 0)
50         return false;
51
52     if (smack_set_label_for_self(newLabel) < 0) {
53         free(newLabel);
54         return false;
55     }
56
57     free(newLabel);
58     return true;
59 }
60
61 bool dropProcessCapability()
62 {
63     // in case of root user, any capabilities aren't dropped
64     if (getuid() == 0)
65         return true;
66
67     cap_user_header_t header;
68     cap_user_data_t data;
69
70     header = static_cast<cap_user_header_t>(malloc(sizeof(*header)));
71     data = static_cast<cap_user_data_t>(calloc(sizeof(*data), _LINUX_CAPABILITY_U32S_3));
72
73     // check if header and data is allocated normally
74     ASSERT(header && data);
75
76     header->pid = getpid();
77     header->version = _LINUX_CAPABILITY_VERSION_3;
78
79     // read already granted capabilities of this process
80     if (capget(header, data) < 0) {
81         free(header);
82         free(data);
83         return false;
84     }
85
86     // remove process capability for CAP_MAC_ADMIN
87     data[CAP_TO_INDEX(CAP_MAC_ADMIN)].inheritable &= ~CAP_TO_MASK(CAP_MAC_ADMIN);
88     data[CAP_TO_INDEX(CAP_MAC_ADMIN)].permitted &= ~CAP_TO_MASK(CAP_MAC_ADMIN);
89     data[CAP_TO_INDEX(CAP_MAC_ADMIN)].effective &= ~CAP_TO_MASK(CAP_MAC_ADMIN);
90
91     bool ret = true;
92     if (capset(header, data) < 0)
93         ret = false;
94
95     free(header);
96     free(data);
97
98     return ret;
99 }
100
101 } // namespace WebKit
102 #endif
103