This a very remarkable quotation from Psalm 40. Here is one of those great prophecies of Christ and if you read it you can see how it works out.
How do we account for the difference between the quotation in Hebrews and the actual words of Psalm 40? As Gouge says, ‘Penmen of the New Testament were not translators of the old, but only quoted [it] for proof of the point in hand; so as they were not tied to syllables and letters, but to the sense.’ The main difference here is in the words in Hebrews, ‘but a body hast thou prepared me’, which the Masoretic text of Psalm 40:6 renders, ‘mine ears hast thou opened’. The reading quoted in Hebrews is the reading according to the Septuagint Greek translation of Psalm 40, and was probably the reading known to readers, which the writer chose to use on the grounds of familiarity. Hebrews speaks of a body because what is being contrasted is the many animal sacrifices of the law and the one sacrifice of Christ which necessitated him having a body and therefore a life to lay down for his people (Hebrews 10:10). Of course, his sacrifice was an offering of a complete human life, body and soul. The mention of his ear being opened may either be a reference to the boring of the ear of the servant who wished to remain in perpetual service (Exodus 21:6), or it may testify to his obedience in hearing all that the Father said to him. In any case an ear without a body is not feasible and it is the body that is relevant to the argument.