sockets.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  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. #include <cutils/log.h>
  17. #include <cutils/sockets.h>
  18. #ifdef HAVE_ANDROID_OS
  19. /* For the socket trust (credentials) check */
  20. #include <private/android_filesystem_config.h>
  21. #endif
  22. bool socket_peer_is_trusted(int fd)
  23. {
  24. #ifdef HAVE_ANDROID_OS
  25. struct ucred cr;
  26. socklen_t len = sizeof(cr);
  27. int n = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cr, &len);
  28. if (n != 0) {
  29. ALOGE("could not get socket credentials: %s\n", strerror(errno));
  30. return false;
  31. }
  32. if ((cr.uid != AID_ROOT) && (cr.uid != AID_SHELL)) {
  33. ALOGE("untrusted userid on other end of socket: userid %d\n", cr.uid);
  34. return false;
  35. }
  36. #endif
  37. return true;
  38. }