summaryrefslogtreecommitdiff
path: root/mysys/my_windac.c
blob: 2c1027e4aa6d43c445ccd0b45213c38f3817d3f6 (plain)
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
/* Copyright (C) 2000-2005 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#include "mysys_priv.h"
#include "m_string.h"
#ifdef __WIN__

/* Windows NT/2000 discretionary access control utility functions. */

/*
  Check if the operating system is built on NT technology.

  RETURN
    0   Windows 95/98/Me
    1   otherwise
*/

static my_bool is_nt()
{
  return GetVersion() < 0x80000000;
}

/*
  Auxilary structure to store pointers to the data which we need to keep
  around while SECURITY_ATTRIBUTES is in use.
*/

typedef struct st_my_security_attr
{
  PSID everyone_sid;
  PACL dacl;
} My_security_attr;


/*
  Allocate and initialize SECURITY_ATTRIBUTES setting up access
  rights for the owner and group `Everybody'.

  SYNOPSIS
    my_security_attr_create()
    psa                [OUT] pointer to store the pointer to SA in
    perror             [OUT] pointer to store error message if there was an
                             error
    owner_rights       [IN]  access rights for the owner
    everyone_rights    [IN]  access rights for group Everybody

  DESCRIPTION
    Set up the security attributes to provide clients with sufficient
    access rights to a kernel object. We need this function
    because if we simply grant all access to everybody (by installing
    a NULL DACL) a mailicious user can attempt a denial of service
    attack by taking ownership over the kernel object. Upon successful
    return `psa' contains a pointer to SECUIRITY_ATTRIBUTES that can be used
    to create kernel objects with proper access rights.

  RETURN
    0  success, psa is 0 or points to a valid SA structure,
       perror is left intact
   !0  error, SA is set to 0, error message is stored in perror
*/

int my_security_attr_create(SECURITY_ATTRIBUTES **psa, const char **perror,
                            DWORD owner_rights, DWORD everyone_rights)
{
  /* Top-level SID authority */
  SID_IDENTIFIER_AUTHORITY world_auth= SECURITY_WORLD_SID_AUTHORITY;
  PSID everyone_sid= 0;
  HANDLE htoken= 0;
  SECURITY_ATTRIBUTES *sa= 0;
  PACL dacl= 0;
  DWORD owner_token_length, dacl_length;
  SECURITY_DESCRIPTOR *sd;
  PTOKEN_USER owner_token;
  PSID owner_sid;
  My_security_attr *attr;

  if (! is_nt())
  {
    *psa= 0;
    return 0;
  }

  /*
    Get SID of Everyone group. Easier to retrieve all SIDs each time
    this function is called than worry about thread safety.
  */
  if (! AllocateAndInitializeSid(&world_auth, 1, SECURITY_WORLD_RID,
                                 0, 0, 0, 0, 0, 0, 0, &everyone_sid))
  {
    *perror= "Failed to retrieve the SID of Everyone group";
    goto error;
  }

  /*
    Get SID of the owner. Using GetSecurityInfo this task can be done
    in just one call instead of five, but GetSecurityInfo declared in
    aclapi.h, so I hesitate to use it.
    SIC: OpenThreadToken works only if there is an active impersonation
    token, hence OpenProcessToken is used.
  */
  if (! OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &htoken))
  {
    *perror= "Failed to retrieve thread access token";
    goto error;
  }
  GetTokenInformation(htoken, TokenUser, 0, 0, &owner_token_length);

  if (! my_multi_malloc(MYF(MY_WME),
                        &sa, ALIGN_SIZE(sizeof(SECURITY_ATTRIBUTES)) +
                             sizeof(My_security_attr),
                        &sd, sizeof(SECURITY_DESCRIPTOR),
                        &owner_token, owner_token_length,
                        0))
  {
    *perror= "Failed to allocate memory for SECURITY_ATTRIBUTES";
    goto error;
  }
  bzero(owner_token, owner_token_length);
  if (! GetTokenInformation(htoken, TokenUser, owner_token,
                            owner_token_length, &owner_token_length))
  {
    *perror= "GetTokenInformation failed";
    goto error;
  }
  owner_sid= owner_token->User.Sid;

  if (! IsValidSid(owner_sid))
  {
    *perror= "IsValidSid failed";
    goto error;
  }

  /* Calculate the amount of memory that must be allocated for the DACL */
  dacl_length= sizeof(ACL) + (sizeof(ACCESS_ALLOWED_ACE)-sizeof(DWORD)) * 2 +
               GetLengthSid(everyone_sid) + GetLengthSid(owner_sid);

  /* Create an ACL */
  if (! (dacl= (PACL) my_malloc(dacl_length, MYF(MY_ZEROFILL|MY_WME))))
  {
    *perror= "Failed to allocate memory for DACL";
    goto error;
  }
  if (! InitializeAcl(dacl, dacl_length, ACL_REVISION))
  {
    *perror= "Failed to initialize DACL";
    goto error;
  }
  if (! AddAccessAllowedAce(dacl, ACL_REVISION, everyone_rights, everyone_sid))
  {
    *perror= "Failed to set up DACL";
    goto error;
  }
  if (! AddAccessAllowedAce(dacl, ACL_REVISION, owner_rights, owner_sid))
  {
    *perror= "Failed to set up DACL";
    goto error;
  }
  if (! InitializeSecurityDescriptor(sd, SECURITY_DESCRIPTOR_REVISION))
  {
    *perror= "Could not initialize security descriptor";
    goto error;
  }
  if (! SetSecurityDescriptorDacl(sd, TRUE, dacl, FALSE))
  {
    *perror= "Failed to install DACL";
    goto error;
  }

  sa->nLength= sizeof(*sa);
  sa->bInheritHandle= TRUE;
  sa->lpSecurityDescriptor= sd;
  /* Save pointers to everyone_sid and dacl to be able to clean them up */
  attr= (My_security_attr*) (((char*) sa) + ALIGN_SIZE(sizeof(*sa)));
  attr->everyone_sid= everyone_sid;
  attr->dacl= dacl;
  *psa= sa;

  CloseHandle(htoken);
  return 0;
error:
  if (everyone_sid)
    FreeSid(everyone_sid);
  if (htoken)
    CloseHandle(htoken);
  my_free((gptr) sa, MYF(MY_ALLOW_ZERO_PTR));
  my_free((gptr) dacl, MYF(MY_ALLOW_ZERO_PTR));
  *psa= 0;
  return 1;
}

/*
  Cleanup security attributes freeing used memory.

  SYNOPSIS
    my_security_attr_free()
    sa   security attributes
*/

void my_security_attr_free(SECURITY_ATTRIBUTES *sa)
{
  if (sa)
  {
    My_security_attr *attr= (My_security_attr*)
                            (((char*)sa) + ALIGN_SIZE(sizeof(*sa)));
    FreeSid(attr->everyone_sid);
    my_free((gptr) attr->dacl, MYF(0));
    my_free((gptr) sa, MYF(0));
  }
}

#endif /* __WIN__ */