This function takes a handle, path and mode, it calls computecon_raw to get the label of the path object if the current process created it, then it calls selabel_lookup to get the default type for the object. It substitutes the default type into label. It tells the SELinux Kernel to label all new file system objects created by the current process with this label. Returns -1 on failure
| 107 | Returns -1 on failure. errno will be set appropriately. |
| 108 | */ |
| 109 | int |
| 110 | defaultcon (struct selabel_handle *selabel_handle, |
| 111 | char const *path, mode_t mode) |
| 112 | { |
| 113 | int rc = -1; |
| 114 | char *scon_raw = NULL; |
| 115 | char *tcon_raw = NULL; |
| 116 | context_t scontext = NULL, tcontext = NULL; |
| 117 | char const *contype; |
| 118 | char const *constr; |
| 119 | char *newpath = NULL; |
| 120 | |
| 121 | if (! IS_ABSOLUTE_FILE_NAME (path)) |
| 122 | { |
| 123 | /* Generate absolute name as required by subsequent selabel_lookup. */ |
| 124 | newpath = canonicalize_filename_mode (path, CAN_MISSING); |
| 125 | if (! newpath) |
| 126 | goto quit; |
| 127 | path = newpath; |
| 128 | } |
| 129 | |
| 130 | if (selabel_lookup_raw (selabel_handle, &scon_raw, path, mode) < 0) |
| 131 | { |
| 132 | /* "No such file or directory" is a confusing error, |
| 133 | when processing files, when in fact it was the |
| 134 | associated default context that was not found. |
| 135 | Therefore map the error to something more appropriate |
| 136 | to the context in which we're using selabel_lookup(). */ |
| 137 | if (errno == ENOENT) |
| 138 | errno = ENODATA; |
| 139 | goto quit; |
| 140 | } |
| 141 | if (computecon_raw (path, mode, &tcon_raw) < 0) |
| 142 | goto quit; |
| 143 | if (!(scontext = context_new (scon_raw))) |
| 144 | goto quit; |
| 145 | if (!(tcontext = context_new (tcon_raw))) |
| 146 | goto quit; |
| 147 | |
| 148 | if (!(contype = context_type_get (scontext))) |
| 149 | goto quit; |
| 150 | if (context_type_set (tcontext, contype)) |
| 151 | goto quit; |
| 152 | if (!(constr = context_str (tcontext))) |
| 153 | goto quit; |
| 154 | |
| 155 | rc = setfscreatecon_raw (constr); |
| 156 | |
| 157 | quit:; |
| 158 | int err = errno; |
| 159 | context_free (scontext); |
| 160 | context_free (tcontext); |
| 161 | freecon (scon_raw); |
| 162 | freecon (tcon_raw); |
| 163 | free (newpath); |
| 164 | errno = err; |
| 165 | return rc; |
| 166 | } |
no test coverage detected