jiabin | 78b761d | 2020-11-25 09:15:38 -0800 | [diff] [blame] | 1 | /* |
| 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/AudioValidator.h> |
| 18 | |
| 19 | namespace android { |
| 20 | |
| 21 | /** returns true if string is overflow */ |
| 22 | template <size_t size> |
| 23 | bool checkStringOverflow(const char (&s)[size]) { |
| 24 | return strnlen(s, size) >= size; |
| 25 | } |
| 26 | |
| 27 | status_t safetyNetLog(status_t status, std::string_view bugNumber) { |
| 28 | if (status != NO_ERROR && !bugNumber.empty()) { |
| 29 | android_errorWriteLog(0x534e4554, bugNumber.data()); // SafetyNet logging |
| 30 | } |
| 31 | return status; |
| 32 | } |
| 33 | |
| 34 | status_t AudioValidator::validateAudioAttributes( |
| 35 | const audio_attributes_t& attr, std::string_view bugNumber) |
| 36 | { |
| 37 | status_t status = NO_ERROR; |
| 38 | const size_t tagsMaxSize = AUDIO_ATTRIBUTES_TAGS_MAX_SIZE; |
| 39 | if (strnlen(attr.tags, tagsMaxSize) >= tagsMaxSize) { |
| 40 | status = BAD_VALUE; |
| 41 | } |
| 42 | return safetyNetLog(status, bugNumber); |
| 43 | } |
| 44 | |
| 45 | status_t AudioValidator::validateEffectDescriptor( |
| 46 | const effect_descriptor_t& desc, std::string_view bugNumber) |
| 47 | { |
| 48 | status_t status = NO_ERROR; |
| 49 | if (checkStringOverflow(desc.name) |
| 50 | | /* always */ checkStringOverflow(desc.implementor)) { |
| 51 | status = BAD_VALUE; |
| 52 | } |
| 53 | return safetyNetLog(status, bugNumber); |
| 54 | } |
| 55 | |
| 56 | status_t AudioValidator::validateAudioPortConfig( |
| 57 | const struct audio_port_config& config, std::string_view bugNumber) |
| 58 | { |
| 59 | status_t status = NO_ERROR; |
| 60 | if (config.type == AUDIO_PORT_TYPE_DEVICE && |
| 61 | checkStringOverflow(config.ext.device.address)) { |
| 62 | status = BAD_VALUE; |
| 63 | } |
| 64 | return safetyNetLog(status, bugNumber); |
| 65 | } |
| 66 | |
| 67 | namespace { |
| 68 | |
| 69 | template <typename T, std::enable_if_t<std::is_same<T, struct audio_port>::value |
| 70 | || std::is_same<T, struct audio_port_v7>::value, int> = 0> |
| 71 | static status_t validateAudioPortInternal(const T& port, std::string_view bugNumber = {}) { |
| 72 | status_t status = NO_ERROR; |
| 73 | if (checkStringOverflow(port.name)) { |
| 74 | status = BAD_VALUE; |
| 75 | } |
| 76 | if (AudioValidator::validateAudioPortConfig(port.active_config) != NO_ERROR) { |
| 77 | status = BAD_VALUE; |
| 78 | } |
| 79 | if (port.type == AUDIO_PORT_TYPE_DEVICE && |
| 80 | checkStringOverflow(port.ext.device.address)) { |
| 81 | status = BAD_VALUE; |
| 82 | } |
| 83 | return safetyNetLog(status, bugNumber); |
| 84 | } |
| 85 | |
| 86 | } // namespace |
| 87 | |
| 88 | status_t AudioValidator::validateAudioPort( |
| 89 | const struct audio_port& port, std::string_view bugNumber) |
| 90 | { |
| 91 | return validateAudioPortInternal(port, bugNumber); |
| 92 | } |
| 93 | |
| 94 | status_t AudioValidator::validateAudioPort( |
| 95 | const struct audio_port_v7& port, std::string_view bugNumber) |
| 96 | { |
| 97 | return validateAudioPortInternal(port, bugNumber); |
| 98 | } |
| 99 | |
| 100 | /** returns BAD_VALUE if sanitization was required. */ |
| 101 | status_t AudioValidator::validateAudioPatch( |
| 102 | const struct audio_patch& patch, std::string_view bugNumber) |
| 103 | { |
| 104 | status_t status = NO_ERROR; |
| 105 | if (patch.num_sources > AUDIO_PATCH_PORTS_MAX) { |
| 106 | status = BAD_VALUE; |
| 107 | } |
| 108 | if (patch.num_sinks > AUDIO_PATCH_PORTS_MAX) { |
| 109 | status = BAD_VALUE; |
| 110 | } |
| 111 | for (size_t i = 0; i < patch.num_sources; i++) { |
| 112 | if (validateAudioPortConfig(patch.sources[i]) != NO_ERROR) { |
| 113 | status = BAD_VALUE; |
| 114 | } |
| 115 | } |
| 116 | for (size_t i = 0; i < patch.num_sinks; i++) { |
| 117 | if (validateAudioPortConfig(patch.sinks[i]) != NO_ERROR) { |
| 118 | status = BAD_VALUE; |
| 119 | } |
| 120 | } |
| 121 | return safetyNetLog(status, bugNumber); |
| 122 | } |
| 123 | |
| 124 | }; // namespace android |