1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
/* -*- mode:c; tab-width:8; c-basic-offset:8; indent-tabs-mode:nil; -*- */
/*
Copyright (C) 2017 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
Portions of this code are copyright 2017 to Primary Data Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef STDC_HEADERS
#include <stddef.h>
#endif
#ifdef HAVE_TIME_H
#include <time.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include "compat.h"
#include "slist.h"
#include "smb2.h"
#include "libsmb2.h"
#include "libsmb2-private.h"
static struct smb2_sid *
decode_sid(struct smb2_context *smb2, void *memctx, struct smb2_iovec *v)
{
struct smb2_sid *sid;
uint8_t revision, sub_auth_count;
int i;
if (v->len < 8) {
smb2_set_error(smb2, "SID must be at least 8 bytes");
return NULL;
}
smb2_get_uint8(v, 0, &revision);
if (revision != 1) {
smb2_set_error(smb2, "can not decode sid with "
"revision %d", revision);
return NULL;
}
smb2_get_uint8(v, 1, &sub_auth_count);
if (v->len < 8 + sub_auth_count * sizeof(uint32_t)) {
smb2_set_error(smb2, "SID is bigger than the buffer");
return NULL;
}
sid = smb2_alloc_data(smb2, memctx,
offsetof(struct smb2_sid, sub_auth) +
sub_auth_count * sizeof(uint32_t));
if (sid == NULL) {
smb2_set_error(smb2, "failed to allocate sid.");
return NULL;
}
sid->revision = revision;
sid->sub_auth_count = sub_auth_count;
memcpy(&sid->id_auth[0], &v->buf[2], SID_ID_AUTH_LEN);
for (i = 0; i < sub_auth_count; i++) {
smb2_get_uint32(v, 8 + i * sizeof(uint32_t),
&sid->sub_auth[i]);
}
v->len -= 8 + sub_auth_count * sizeof(uint32_t);
v->buf += 8 + sub_auth_count * sizeof(uint32_t);
return sid;
}
static struct smb2_ace *
decode_ace(struct smb2_context *smb2, void *memctx, struct smb2_iovec *vec)
{
struct smb2_iovec v = *vec;
uint8_t ace_type, ace_flags;
uint16_t ace_size;
struct smb2_ace *ace;
if (v.len < 4) {
smb2_set_error(smb2, "not enough data for ace header.");
return NULL;
}
smb2_get_uint8(&v, 0, &ace_type);
smb2_get_uint8(&v, 1, &ace_flags);
smb2_get_uint16(&v, 2, &ace_size);
ace = smb2_alloc_data(smb2, memctx, sizeof(struct smb2_ace));
if (ace == NULL) {
smb2_set_error(smb2, "failed to allocate ace.");
return NULL;
}
ace->ace_type = ace_type;
ace->ace_flags = ace_flags;
ace->ace_size = ace_size;
/* Skip past the header */
if (ace_size < 4) {
smb2_set_error(smb2, "not enough data for ace data.");
return NULL;
}
if (v.len < ace_size) {
smb2_set_error(smb2, "not enough data for ace data.");
return NULL;
}
v.len -= 4;
v.buf = &v.buf[4];
/* decode the content of the ace */
/* TODO: have a default case where we just keep the raw blob */
switch (ace_type) {
case SMB2_ACCESS_ALLOWED_ACE_TYPE:
case SMB2_ACCESS_DENIED_ACE_TYPE:
case SMB2_SYSTEM_AUDIT_ACE_TYPE:
case SMB2_SYSTEM_MANDATORY_LABEL_ACE_TYPE:
case SMB2_SYSTEM_SCOPED_POLICY_ID_ACE_TYPE:
smb2_get_uint32(&v, 0, &ace->mask);
if (v.len < 4) {
smb2_set_error(smb2, "not enough data for ace data.");
return NULL;
}
v.len -= 4;
v.buf = &v.buf[4];
ace->sid = decode_sid(smb2, memctx, &v);
break;
case SMB2_ACCESS_ALLOWED_OBJECT_ACE_TYPE:
case SMB2_ACCESS_DENIED_OBJECT_ACE_TYPE:
case SMB2_SYSTEM_AUDIT_OBJECT_ACE_TYPE:
if (v.len < 40) {
smb2_set_error(smb2, "not enough data for ace data.");
return NULL;
}
smb2_get_uint32(&v, 0, &ace->mask);
v.len -= 4;
v.buf = &v.buf[4];
smb2_get_uint32(&v, 0, &ace->flags);
v.len -= 4;
v.buf = &v.buf[4];
memcpy(ace->object_type, v.buf, SMB2_OBJECT_TYPE_SIZE);
v.len -= SMB2_OBJECT_TYPE_SIZE;
v.buf = &v.buf[SMB2_OBJECT_TYPE_SIZE];
memcpy(ace->inherited_object_type, v.buf,
SMB2_OBJECT_TYPE_SIZE);
v.len -= SMB2_OBJECT_TYPE_SIZE;
v.buf = &v.buf[SMB2_OBJECT_TYPE_SIZE];
ace->sid = decode_sid(smb2, memctx, &v);
break;
case SMB2_ACCESS_ALLOWED_CALLBACK_ACE_TYPE:
case SMB2_ACCESS_DENIED_CALLBACK_ACE_TYPE:
case SMB2_SYSTEM_RESOURCE_ATTRIBUTE_ACE_TYPE:
smb2_get_uint32(&v, 0, &ace->mask);
if (v.len < 4) {
smb2_set_error(smb2, "not enough data for ace data.");
return NULL;
}
v.len -= 4;
v.buf = &v.buf[4];
ace->sid = decode_sid(smb2, memctx, &v);
ace->ad_len = v.len;
ace->ad_data = smb2_alloc_data(smb2, memctx, ace->ad_len);
if (ace->ad_data == NULL) {
return NULL;
}
memcpy(ace->ad_data, v.buf, v.len);
break;
default:
ace->raw_len = v.len;
ace->raw_data = smb2_alloc_data(smb2, memctx, ace->raw_len);
if (ace->raw_data == NULL) {
return NULL;
}
memcpy(ace->raw_data, v.buf, v.len);
}
return ace;
}
static struct smb2_acl *
decode_acl(struct smb2_context *smb2, void *memctx, struct smb2_iovec *vec)
{
struct smb2_iovec v = *vec;
struct smb2_acl *acl;
uint8_t revision;
uint16_t acl_size, ace_count;
int i;
if (v.len < 8) {
smb2_set_error(smb2, "not enough data for acl header.");
return NULL;
}
smb2_get_uint8(&v, 0, &revision);
smb2_get_uint16(&v, 2, &acl_size);
smb2_get_uint16(&v, 4, &ace_count);
switch (revision) {
case SMB2_ACL_REVISION:
case SMB2_ACL_REVISION_DS:
break;
default:
smb2_set_error(smb2, "can not decode acl with "
"revision %d", revision);
return NULL;
}
if (v.len > acl_size) {
v.len = acl_size;
}
if (v.len < acl_size) {
smb2_set_error(smb2, "not enough data for acl");
return NULL;
}
acl = smb2_alloc_data(smb2, memctx, sizeof(struct smb2_acl));
if (acl == NULL) {
smb2_set_error(smb2, "failed to allocate acl.");
return NULL;
}
acl->revision = revision;
acl->ace_count = ace_count;
/* Skip past the ACL header to the first ace. */
v.len -= 8;
v.buf = &v.buf[8];
for (i = 0; i < ace_count; i++) {
struct smb2_ace *ace = decode_ace(smb2, memctx, &v);
if (ace == NULL) {
smb2_set_error(smb2, "failed to decode ace # %d: %s",
i, smb2_get_error(smb2));
return NULL;
}
/* skip to the next ace */
if (ace->ace_size > v.len) {
smb2_set_error(smb2, "not enough data for ace %s",
smb2_get_error(smb2));
return NULL;
}
v.len -= ace->ace_size;
v.buf = &v.buf[ace->ace_size];
SMB2_LIST_ADD_END(&acl->aces, ace);
}
return acl;
}
int
smb2_decode_security_descriptor(struct smb2_context *smb2,
void *memctx,
struct smb2_security_descriptor *sd,
struct smb2_iovec *vec)
{
struct smb2_iovec v;
uint32_t offset_owner, offset_group, offset_sacl, offset_dacl;
if (vec->len < 20) {
return -1;
}
v.buf = &vec->buf[0];
v.len = 20;
smb2_get_uint8(&v, 0, &sd->revision);
if (sd->revision != 1) {
smb2_set_error(smb2, "can not decode security descriptor with "
"revision %d", sd->revision);
return -1;
}
smb2_get_uint16(&v, 2, &sd->control);
smb2_get_uint32(&v, 4, &offset_owner);
smb2_get_uint32(&v, 8, &offset_group);
smb2_get_uint32(&v, 12, &offset_sacl);
smb2_get_uint32(&v, 16, &offset_dacl);
/* Owner */
if (offset_owner > 0 && offset_owner + 2 + SID_ID_AUTH_LEN < vec->len) {
v.buf = &vec->buf[offset_owner];
v.len = vec->len - offset_owner;
sd->owner = decode_sid(smb2, memctx, &v);
if (sd->owner == NULL) {
smb2_set_error(smb2, "failed to decode owner sid: %s",
smb2_get_error(smb2));
return -1;
}
}
/* Group */
if (offset_group > 0 && offset_group + 2 + SID_ID_AUTH_LEN < vec->len) {
v.buf = &vec->buf[offset_group];
v.len = vec->len - offset_group;
sd->group = decode_sid(smb2, sd, &v);
if (sd->group == NULL) {
smb2_set_error(smb2, "failed to decode group sid: %s",
smb2_get_error(smb2));
return -1;
}
}
/* DACL */
if (offset_dacl > 0 && offset_dacl + 8 <= vec->len) {
v.buf = &vec->buf[offset_dacl];
v.len = vec->len - offset_dacl;
sd->dacl = decode_acl(smb2, sd, &v);
if (sd->dacl == NULL) {
smb2_set_error(smb2, "failed to decode dacl: %s",
smb2_get_error(smb2));
return -1;
}
}
return 0;
}
|