blob: 44ca956107543532da28b6b67c70f8541ad30a24 [file] [log] [blame]
Eric Laurent4dacbc32020-10-07 13:48:21 -07001/*
2 * Copyright (C) 2020 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#include <media/AudioSanitizer.h>
18
19namespace android {
20
21 /** returns true if string overflow was prevented by zero termination */
22template <size_t size>
23bool preventStringOverflow(char (&s)[size]) {
24 if (strnlen(s, size) < size) return false;
25 s[size - 1] = '\0';
26 return true;
27}
28
29status_t safetyNetLog(status_t status, const char *bugNumber) {
30 if (status != NO_ERROR && bugNumber != nullptr) {
31 android_errorWriteLog(0x534e4554, bugNumber); // SafetyNet logging
32 }
33 return status;
34}
35
36status_t AudioSanitizer::sanitizeAudioAttributes(
37 audio_attributes_t *attr, const char *bugNumber)
38{
39 status_t status = NO_ERROR;
40 const size_t tagsMaxSize = AUDIO_ATTRIBUTES_TAGS_MAX_SIZE;
41 if (strnlen(attr->tags, tagsMaxSize) >= tagsMaxSize) {
42 status = BAD_VALUE;
43 }
44 attr->tags[tagsMaxSize - 1] = '\0';
45 return safetyNetLog(status, bugNumber);
46}
47
48/** returns BAD_VALUE if sanitization was required. */
49status_t AudioSanitizer::sanitizeEffectDescriptor(
50 effect_descriptor_t *desc, const char *bugNumber)
51{
52 status_t status = NO_ERROR;
53 if (preventStringOverflow(desc->name)
54 | /* always */ preventStringOverflow(desc->implementor)) {
55 status = BAD_VALUE;
56 }
57 return safetyNetLog(status, bugNumber);
58}
59
60/** returns BAD_VALUE if sanitization was required. */
61status_t AudioSanitizer::sanitizeAudioPortConfig(
62 struct audio_port_config *config, const char *bugNumber)
63{
64 status_t status = NO_ERROR;
65 if (config->type == AUDIO_PORT_TYPE_DEVICE &&
66 preventStringOverflow(config->ext.device.address)) {
67 status = BAD_VALUE;
68 }
69 return safetyNetLog(status, bugNumber);
70}
71
72/** returns BAD_VALUE if sanitization was required. */
73status_t AudioSanitizer::sanitizeAudioPort(
74 struct audio_port *port, const char *bugNumber)
75{
76 status_t status = NO_ERROR;
77 if (preventStringOverflow(port->name)) {
78 status = BAD_VALUE;
79 }
80 if (sanitizeAudioPortConfig(&port->active_config) != NO_ERROR) {
81 status = BAD_VALUE;
82 }
83 if (port->type == AUDIO_PORT_TYPE_DEVICE &&
84 preventStringOverflow(port->ext.device.address)) {
85 status = BAD_VALUE;
86 }
87 return safetyNetLog(status, bugNumber);
88}
89
90/** returns BAD_VALUE if sanitization was required. */
91status_t AudioSanitizer::sanitizeAudioPatch(
92 struct audio_patch *patch, const char *bugNumber)
93{
94 status_t status = NO_ERROR;
95 if (patch->num_sources > AUDIO_PATCH_PORTS_MAX) {
96 patch->num_sources = AUDIO_PATCH_PORTS_MAX;
97 status = BAD_VALUE;
98 }
99 if (patch->num_sinks > AUDIO_PATCH_PORTS_MAX) {
100 patch->num_sinks = AUDIO_PATCH_PORTS_MAX;
101 status = BAD_VALUE;
102 }
103 for (size_t i = 0; i < patch->num_sources; i++) {
104 if (sanitizeAudioPortConfig(&patch->sources[i]) != NO_ERROR) {
105 status = BAD_VALUE;
106 }
107 }
108 for (size_t i = 0; i < patch->num_sinks; i++) {
109 if (sanitizeAudioPortConfig(&patch->sinks[i]) != NO_ERROR) {
110 status = BAD_VALUE;
111 }
112 }
113 return safetyNetLog(status, bugNumber);
114}
115
116}; // namespace android