|
| 1 | +/**************************************************************************** |
| 2 | + * binfmt/binfmt_checkexec.c |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 7 | + * contributor license agreements. See the NOTICE file distributed with |
| 8 | + * this work for additional information regarding copyright ownership. The |
| 9 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 10 | + * "License"); you may not use this file except in compliance with the |
| 11 | + * License. You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 18 | + * License for the specific language governing permissions and limitations |
| 19 | + * under the License. |
| 20 | + * |
| 21 | + ****************************************************************************/ |
| 22 | + |
| 23 | +/**************************************************************************** |
| 24 | + * Included Files |
| 25 | + ****************************************************************************/ |
| 26 | + |
| 27 | +#include <nuttx/config.h> |
| 28 | + |
| 29 | +#include <sys/stat.h> |
| 30 | +#include <errno.h> |
| 31 | + |
| 32 | +#include <nuttx/sched.h> |
| 33 | +#include <nuttx/binfmt/binfmt.h> |
| 34 | + |
| 35 | +#include "binfmt.h" |
| 36 | + |
| 37 | +/**************************************************************************** |
| 38 | + * Public Functions |
| 39 | + ****************************************************************************/ |
| 40 | + |
| 41 | +/**************************************************************************** |
| 42 | + * Name: binfmt_checkexecperm |
| 43 | + * |
| 44 | + * Description: |
| 45 | + * Verify that the calling task has execute permission on the file |
| 46 | + * described by 'bin'. The file owner, group, and mode must already be |
| 47 | + * populated in the binary_s structure before calling this function. |
| 48 | + * |
| 49 | + * Input Parameters: |
| 50 | + * bin - Pointer to the binary descriptor with uid, gid, and mode set. |
| 51 | + * |
| 52 | + * Returned Value: |
| 53 | + * Zero (OK) on success; -EACCES if execute permission is denied. |
| 54 | + * |
| 55 | + ****************************************************************************/ |
| 56 | + |
| 57 | +int binfmt_checkexecperm(FAR struct binary_s *bin) |
| 58 | +{ |
| 59 | + FAR struct tcb_s *rtcb; |
| 60 | + mode_t xbits; |
| 61 | + |
| 62 | + rtcb = nxsched_self(); |
| 63 | + |
| 64 | + if (bin == NULL || rtcb == NULL || rtcb->group == NULL) |
| 65 | + { |
| 66 | + return OK; |
| 67 | + } |
| 68 | + |
| 69 | + if (rtcb->group->tg_euid == 0) |
| 70 | + { |
| 71 | + /* Root can execute any file that has at least one execute bit set */ |
| 72 | + |
| 73 | + if ((bin->mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0) |
| 74 | + { |
| 75 | + return -EACCES; |
| 76 | + } |
| 77 | + |
| 78 | + return OK; |
| 79 | + } |
| 80 | + |
| 81 | + if (rtcb->group->tg_euid == bin->uid) |
| 82 | + { |
| 83 | + xbits = S_IXUSR; |
| 84 | + } |
| 85 | + else if (rtcb->group->tg_egid == bin->gid) |
| 86 | + { |
| 87 | + xbits = S_IXGRP; |
| 88 | + } |
| 89 | + else |
| 90 | + { |
| 91 | + xbits = S_IXOTH; |
| 92 | + } |
| 93 | + |
| 94 | + if ((bin->mode & xbits) == 0) |
| 95 | + { |
| 96 | + return -EACCES; |
| 97 | + } |
| 98 | + |
| 99 | + return OK; |
| 100 | +} |
0 commit comments