blob: 15ede2b5b69ffb3fc41aefc27668f99877314471 [file] [log] [blame]
jeffhao2b8f76c2011-05-05 14:25:36 -07001/*
2 * Copyright (C) 2011 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
jeffhao2b8f76c2011-05-05 14:25:36 -070017#include <cutils/sockets.h>
Mark Salyzyn12717162014-04-29 15:49:14 -070018#include <log/log.h>
jeffhao2b8f76c2011-05-05 14:25:36 -070019
20#ifdef HAVE_ANDROID_OS
21/* For the socket trust (credentials) check */
22#include <private/android_filesystem_config.h>
Mark Salyzyn12717162014-04-29 15:49:14 -070023#define __android_unused
24#else
25#define __android_unused __attribute__((__unused__))
jeffhao2b8f76c2011-05-05 14:25:36 -070026#endif
27
Mark Salyzyn12717162014-04-29 15:49:14 -070028bool socket_peer_is_trusted(int fd __android_unused)
jeffhao2b8f76c2011-05-05 14:25:36 -070029{
30#ifdef HAVE_ANDROID_OS
31 struct ucred cr;
32 socklen_t len = sizeof(cr);
33 int n = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
34
35 if (n != 0) {
Steve Block01dda202012-01-06 14:13:42 +000036 ALOGE("could not get socket credentials: %s\n", strerror(errno));
jeffhao2b8f76c2011-05-05 14:25:36 -070037 return false;
38 }
39
40 if ((cr.uid != AID_ROOT) && (cr.uid != AID_SHELL)) {
Steve Block01dda202012-01-06 14:13:42 +000041 ALOGE("untrusted userid on other end of socket: userid %d\n", cr.uid);
jeffhao2b8f76c2011-05-05 14:25:36 -070042 return false;
43 }
44#endif
45
46 return true;
47}