This file is indexed.

/usr/src/open-vm-tools-10.0.7/vmhgfs/hgfsBd.c is in open-vm-tools-dkms 2:10.0.7-3227872-2ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*********************************************************
 * Copyright (C) 1998-2015 VMware, Inc. All rights reserved.
 *
 * 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 version 2.1 and no 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 Lesser GNU 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *********************************************************/

/*********************************************************
 * The contents of this file are subject to the terms of the Common
 * Development and Distribution License (the "License") version 1.0
 * and no later version.  You may not use this file except in
 * compliance with the License.
 *
 * You can obtain a copy of the License at
 *         http://www.opensource.org/licenses/cddl1.php
 *
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 *********************************************************/

/*
 * hgfsBd.c --
 *
 *    Backdoor calls used by hgfs pserver. [bac]
 */

#if defined(__KERNEL__) || defined(_KERNEL) || defined(KERNEL)
#   include "kernelStubs.h"
#else
#   include <stdio.h>
#   include <stdlib.h>
#   include <string.h>
#   include <errno.h>
#   include "str.h"      // for Str_Strcpy
#   include "debug.h"
#endif

#include "vm_assert.h"
#include "rpcout.h"
#include "hgfs.h"     // for common HGFS definitions
#include "hgfsBd.h"


/*
 *-----------------------------------------------------------------------------
 *
 * HgfsBdGetBufInt --
 *
 *    Allocates a buffer to send a hgfs request in. This can be either a
 *    HGFS_PACKET_MAX or HGFS_LARGE_PACKET_MAX size buffer depending on the
 *    external funciton called.
 *
 * Results:
 *    Pointer to a buffer that has the correct backdoor command prefix for 
 *    sending hgfs requests over the backdoor.
 *    NULL on failure (not enough memory).
 *
 * Side effects:
 *    None.
 *
 *-----------------------------------------------------------------------------
 */

static char *
HgfsBdGetBufInt(size_t bufSize)
{
   /* 
    * Allocate a buffer that is large enough for an HGFS packet and the 
    * synchronous HGFS command, write the command, and return a pointer that 
    * points into the buffer, after the command.
    */
   size_t len = bufSize + HGFS_SYNC_REQREP_CLIENT_CMD_LEN;
   char *buf = (char*) calloc(sizeof(char), len);

   if (!buf) {
      Debug("HgfsBd_GetBuf: Failed to allocate a bd buffer\n");
      return NULL;
   }

   Str_Strcpy(buf, HGFS_SYNC_REQREP_CLIENT_CMD, len);

   return buf + HGFS_SYNC_REQREP_CLIENT_CMD_LEN;
}


/*
 *-----------------------------------------------------------------------------
 *
 * HgfsBd_GetBuf --
 *
 *    Get a buffer of size HGFS_PACKET_MAX to send hgfs requests in.
 *
 * Results:
 *    See HgfsBdGetBufInt.
 *
 * Side effects:
 *    Allocates memory that must be freed with a call to HgfsBd_PutBuf.
 *
 *-----------------------------------------------------------------------------
 */

char *
HgfsBd_GetBuf(void)
{
   return HgfsBdGetBufInt(HGFS_PACKET_MAX);
}

/*
 *-----------------------------------------------------------------------------
 *
 * HgfsBd_GetLargeBuf --
 *
 *    Get a buffer of size HGFS_LARGE_PACKET_MAX to send hgfs requests in.
 *
 * Results:
 *    See HgfsBdGetBufInt.
 *
 * Side effects:
 *    Allocates memory that must be freed with a call to HgfsBd_PutBuf.
 *
 *-----------------------------------------------------------------------------
 */

char *
HgfsBd_GetLargeBuf(void)
{
   return HgfsBdGetBufInt(HGFS_LARGE_PACKET_MAX);
}


/*
 *-----------------------------------------------------------------------------
 *
 * HgfsBd_PutBuf --
 *
 *    Release a buffer obtained with HgfsBd_GetBuf.
 *
 * Results:
 *    None
 *
 * Side effects:
 *    None
 *
 *-----------------------------------------------------------------------------
 */

void
HgfsBd_PutBuf(char *buf) // IN
{
   ASSERT(buf);

   free(buf - HGFS_SYNC_REQREP_CLIENT_CMD_LEN);
}


/*
 *-----------------------------------------------------------------------------
 *
 * HgfsBd_GetChannel --
 *
 *    Allocate a new RpcOut channel, and try to open the connection.
 *
 * Results:
 *    Pointer to the allocated, opened channel on success.
 *    NULL on failure (not enough memory, or failed to open the connection).
 *
 * Side effects:
 *    None
 *
 *-----------------------------------------------------------------------------
 */

RpcOut *
HgfsBd_GetChannel(void)
{
   RpcOut *out = RpcOut_Construct();
   Bool status;

   if (!out) {
      Debug("HgfsBd_GetChannel: Failed to allocate an RpcOut\n");
      return NULL;
   }

   status = RpcOut_start(out);
   if (status == FALSE) {
      RpcOut_Destruct(out);
      return NULL;
   }

   return out;
}


/*
 *-----------------------------------------------------------------------------
 *
 * HgfsBd_CloseChannel --
 *
 *    Close the channel and free the RpcOut object.
 *
 * Results:
 *    TRUE if closing the channel succeeded, FALSE if it failed.
 *
 * Side effects:
 *    None
 *
 *-----------------------------------------------------------------------------
 */

Bool
HgfsBd_CloseChannel(RpcOut *out) // IN: Channel to close and free
{
   Bool success; 

   ASSERT(out);

   success = RpcOut_stop(out);
   if (success == TRUE) {
      RpcOut_Destruct(out);
   }

   return success;
}


/*
 *-----------------------------------------------------------------------------
 *
 * HgfsBd_Dispatch --
 *
 *    Get a reply to an hgfs request. We call RpcOut_Sent, which
 *    returns a buffer with the reply in it, and we pass this back to
 *    the caller.
 *
 * Results:
 *    On success, returns zero. On failure, returns a negative error.
 *
 * Side effects:
 *    None
 *
 *-----------------------------------------------------------------------------
 */

int
HgfsBd_Dispatch(RpcOut *out,            // IN: Channel to send on
                char *packetIn,         // IN: Buf containing request packet
                size_t *packetSize,     // IN/OUT: Size of packet in/out
                char const **packetOut) // OUT: Buf containing reply packet
{
   Bool success;
   char const *reply;
   size_t replyLen;
   char *bdPacket = packetIn - HGFS_SYNC_REQREP_CLIENT_CMD_LEN;

   ASSERT(out);
   ASSERT(packetIn);
   ASSERT(packetSize);
   ASSERT(packetOut);

   memcpy(bdPacket, HGFS_SYNC_REQREP_CLIENT_CMD, HGFS_SYNC_REQREP_CLIENT_CMD_LEN);

   success = RpcOut_send(out, bdPacket, *packetSize + HGFS_CLIENT_CMD_LEN,
                         &reply, &replyLen);
   if (success == FALSE) {
      Debug("HgfsBd_Dispatch: RpcOut_send returned failure\n");
      return -1;
   }

   ASSERT(replyLen <= HGFS_LARGE_PACKET_MAX);
   *packetOut = reply;
   *packetSize = replyLen;

   return 0;
}


/*
 *-----------------------------------------------------------------------------
 *
 * HgfsBd_Enabled --
 *
 *    Test to see if hgfs is enabled on the host.
 *
 * Results:
 *    TRUE if hgfs is enabled.
 *    FALSE if hgfs is disabled.
 *
 * Side effects:
 *    None
 *
 *-----------------------------------------------------------------------------
 */

Bool
HgfsBd_Enabled(RpcOut *out,         // IN: RPCI Channel
               char *requestPacket) // IN: Buffer (obtained from HgfsBd_GetBuf)
{
   char const *replyPacket; // Buffer returned by HgfsBd_Dispatch
   size_t replyLen;
   Bool success;

   /*
    * Send a bogus (empty) request to the VMX. If hgfs is disabled on
    * the host side then the request will fail (because the RPCI call
    * itself will fail). If hgfs is enabled, we will get a packet back
    * (it will be an error packet because our request was malformed,
    * but we just discard it anyway).
    */
   success = RpcOut_send(out, requestPacket - HGFS_CLIENT_CMD_LEN,
                         HGFS_CLIENT_CMD_LEN,
                         &replyPacket, &replyLen);
   if (success == TRUE) {
      ASSERT(replyLen <= HGFS_LARGE_PACKET_MAX);
   }

   return success;
}


/*
 *-----------------------------------------------------------------------------
 *
 * HgfsBd_OpenBackdoor --
 *
 *      Check if the HGFS channel is open, and, if not, open it. This is a
 *      one-stop convenience wrapper around HgfsBd_Enabled, HgfsBd_GetBuf, and
 *      HgfsBd_GetChannel.
 *
 * Results:
 *      TRUE if the backdoor is now open, regardless of its previous state.
 *      FALSE if the backdoor could not be opened.
 *
 * Side effects:
 *      May open a channel to the host.
 *
 *-----------------------------------------------------------------------------
 */

Bool
HgfsBd_OpenBackdoor(RpcOut **out) // IN/OUT: RPCI Channel
{
   char *packetBuffer = NULL;
   Bool success = FALSE;

   ASSERT(out);

   /* Short-circuit: backdoor is already open. */
   if (*out != NULL) {
      return TRUE;
   }

   /* Open the channel. */   
   *out = HgfsBd_GetChannel();
   if (*out == NULL) {
      return FALSE;
   }

   /* Allocate a buffer for use in pinging the HGFS server. */
   packetBuffer = HgfsBd_GetBuf();
   if (packetBuffer == NULL) {
      goto out;
   }

   /* Ping the HGFS server. */
   if (!HgfsBd_Enabled(*out, packetBuffer)) {
      goto out;
   }
   success = TRUE;

  out:
   if (packetBuffer != NULL) {
      HgfsBd_PutBuf(packetBuffer);
   }
   if (!success && *out != NULL) {
      HgfsBd_CloseChannel(*out);
      *out = NULL;
   }
   return success;
}


/*
 *-----------------------------------------------------------------------------
 *
 * HgfsBd_CloseBackdoor --
 *
 *      Closes the backdoor channel, if it's open.
 *
 * Results:
 *      TRUE if the channel is now closed, regardless of its previous state.
 *      FALSE if we could not close the channel.
 *
 * Side effects:
 *      May close the channel to the host.
 *
 *-----------------------------------------------------------------------------
 */

Bool
HgfsBd_CloseBackdoor(RpcOut **out) // IN/OUT: RPCI Channel
{
   Bool success = TRUE;

   ASSERT(out);

   if (*out != NULL) {
      if (!HgfsBd_CloseChannel(*out)) {
         success = FALSE;
      }
      *out = NULL;
   }

   return success;
}